The DataColumn Class

Team-Fly team-fly    

 
ADO.NET Programming in Visual Basic .NET
By Steve  Holzner, Bob  Howell

Table of Contents
Chapter 7.   The ADO .NET DataSet


The DataColumn class is used to store the metadata about each column. Metadata is information that describes the data in the table. This class includes properties such as the data type, column name , and data size . Each DataTable contains a collection of DataColumn objects (an instance of DataColumnCollection) accessible through the Columns property.

The Columns Collection

The DataColumnCollection class has properties and methods of its own for managing the collection of columns it contains. These include the Count property, and methods for adding/deleting members of the collection. Let's look at some of the properties and methods.

  • Count The number of items in the collection.

  • Item Returns the DataColumn object at the specified index. The index can be numeric or a string that is the column name.

  • IsReadOnly If True means that you can not modify the collection. This is always the case when using typed DataSets. Untyped DataSets column collections can be manipulated in code, typed cannot. By testing this property, you can avoid runtime errors

  • Add Adds a DataColumn object to the collection. You can build your DataColumn using a variable and then add it, or you can supply parameters to the method such as name, data type, and so forth. Returns a reference to the DataColumn object just created.

  • AddRange Adds a range of DataColumn objects passed as an array.

  • CanRemove Returns True if a DataColumn can be removed.

  • Clear Clears the collection. Does not work on typed DataSets as the collection is read-only.

  • Remove, RemoveAt Removes a column with the specified column name (Remove) or with the specified index (RemoveAt).

  • IndexOf Returns the numerical index of the column with the specified name.

DataColumn Properties and Methods

The following properties and methods apply to the DataColumn class itself. Not all properties and methods are listed, just the more commonly used ones.

  • AllowDBNull Whether to allow the column to have the null value.

  • AutoIncrement Indicates that this column will increment its value automatically when new rows are added.

  • AutoIncrementSeed Starting number to increment.

  • AutoIncrementStep Sets the increment to use when generating the value of the column.

  • Caption The caption of the column. This can be used as a field label on a form or report.

  • ColumnName The name of the column.

  • DataType Returns a type object that contains the data type of the column. To get a string representation of the type you must use its ToString method. To assign a data type to the property, you must use the GetType method of the System.Type class.

  • DefaultValue The value to use when creating new rows if no value is supplied for the column.

  • Expression Enables calculations to be performed on columns. More on this later in the chapter.

  • MaxLength The maximum length of a text column.

  • Unique Indicates whether the values in each row in the table must be unique.

The DataRow Class

The DataRow class is used to hold each row of data in the table. The correspondent collection, the DataRowCollection, is used to hold all of the DataRows in the DataSet. Since I have the rows available in a standard collection, there is no need to navigate the table using methods such as Move Next, Previous, and so forth as with the old RecordSet object. You can either use an indexing variable and the for loop to navigate the rows or you can use an enumerator with the For Each Next construct. Each DataRow is an object itself with its own properties and methods. Let's look at them now.

DataRowCollection Properties and Methods

The following methods are on the DataRowCollection class which is returned from the Rows property of the DataTable class:

  • Count The number of rows in the collection.

  • Add Adds a new row to the collection.

  • Clear Removes all of the rows from the DataTable. This method effectively deletes all of the data in the table.

  • Contains This function is very handy. The program passes it an array of values and it returns True if any of the values are contained in the primary key of the table. This is very useful if you just want to determine if any rows contain a value or set of values. Sometimes this is all you need to determine if data exists.

  • Delete Marks the row as deleted. This is different from Remove/RemoveAt because when a row is marked as deleted, it is not actually removed from the collection. The Update method of the DataAdapter can then be used to propagate the deletions back to the database. If you remove a row, it is gone. The Update method will not work.

  • Find This is the equivalent of a direct look up. The programmer passes this function a value and it returns the row that contains that value as its primary key. The function accepts an array of values for multiple-column primary keys.

  • InsertAt This method inserts an already formed row into the collection at a specified location.

  • Remove Removes an existing row from the collection. The caveat about this function is that it requires that you pass it a Row object. See the Delete method for comments

  • RemoveAt Removes the row at the specified index from the collection. See the Delete method for comments.

DataRow Class Properties and Methods

The DataRow class has the following properties and methods.

  • HasErrors Gets a value that tells whether there is an error in the row.

  • Item, ItemArray Returns the value of a column in the row, or returns an array of values of all of the columns in the row. The Item property can be retrieved using either an index or a text key that corresponds to the column name.

  • RowState Gets a value that tells whether or not the row is being edited.

  • AcceptChanges, RejectChanges Accepts or rejects changes made to the row since a last call to Accept or Reject changes.

  • BeginEdit Places the row into a state where changes can be made to it.

  • CancelEdit Cancels any edits made to the row and returns it to the state it was in before the user called BeginEdit.

  • EndEdit Ends the edit of the row and makes the changes permanent.

  • GetChildRows Gets any child rows in a master-detail relationship. Each row in a master table may have many rows in the child table. When navigating the DataTable it is important to refresh the child rows each time you reference a new master row.

  • GetParentRows Returns the parent row of a child row in a master-detail relationship.

  • GetColumnsInError Returns an array of columns with associated errors.

  • HasVersion Returns True if any column in the row has changes in it.

  • IsNull Returns True if the column in a row has the Null value.

  • SetNull Sets a column in a row to the Null value. You cannot assign the null value to a column as you could in Visual Basic 6. You must use this method instead.

The DataView Class

The DataView class is what we call a helper class. It isn't really necessary for normal functionality, but it helps us perform certain functions that would be difficult other ways. The two most useful features of the class are its ability to sort and filter the rows in a DataTable. The other properties that are useful are AllowEdit, AllowNew, and AllowDelete. You can use the DataView class as a security control object, by creating DataViews with different access rights and then assigning them based on the user's rights. Here are the common properties and methods of the DataView class.

  • AllowEdit, AllowNew, AllowDelete These properties are useful for security control. When the DataView is a flight to the DataTable and these properties are set, the user will only be allowed to perform the functions indicated.

  • ApplyDefaultSort When set to True this property removes any sort that has been placed on the DataView and reverts to the default sort order.

  • Count Returns the number of rows after the RowStateFilter and RowFilter has been applied.

  • RowFilter This property determines which rows appear after a filter is applied. The filter can be any Boolean expression. The filter is similar to what you find in the where clause of an SQL statement. The difference between this property and the Find method on the DataRowCollection is that this property only limits the rows that are visible through the DataView. It does not change the underlying DataTable in any way.

  • RowStateFilter The RowState indicates whether the row is in Edit mode, being added, and so forth. The RowStateFilter is useful for filtering rows based on the RowState value.

  • Sort This function accepts a comma-separated values list of fields to sort by. This is passed as a string and is similar to an SQL Order By clause. As with an Order By clause you can also use the DESC (descending) and ASC ( ascending ) modifiers.

  • AddNew Adds a new blank DataRowView object to the DataView. The DataRowView object that is returned is empty. The DataRowView class provides a customized view of each DataRow.

  • Delete Deletes a row from the DataView.

  • Find, FindRows Finds a row or rows by its specified sort key value.

Now that we're familiar with the properties and methods of the DataSet and its associated classes, let's look at examples of how to use the classes.


Team-Fly team-fly    
Top


ADO. NET Programming in Visual Basic. NET
ADO.NET Programming in Visual Basic .NET (2nd Edition)
ISBN: 0131018817
EAN: 2147483647
Year: 2005
Pages: 123

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net