|
|
You use an object of the DataView class to view only specific rows in a DataTable object using a filter. You can also sort the rows viewed by a DataView. You can add, modify, and remove rows from a DataView, and those changes will also be applied to the underlying DataTable that the DataView reads from; you'll learn more about this later in the section "Adding, Modifying, and Removing DataRowView Objects from a DataView." Table 13.1 shows some of the DataView properties, and Table 13.2 shows some of the DataView methods.
PROPERTY | TYPE | DESCRIPTION |
---|---|---|
AllowDelete | bool | Gets or sets a bool that indicates whether deletion of DataRowView objects from your DataView is permitted. The default is true . |
AllowEdit | bool | Gets or sets a bool that indicates whether editing of DataRowView objects in your DataView is permitted. The default is true. |
AllowNew | bool | Gets or sets a bool that indicates whether adding new DataRowView objects to your DataView is permitted. The default is true. |
ApplyDefaultSort | bool | Gets or sets a bool that indicates whether to use the default sorting algorithm to sort rows in your DataView. When set to true, the default sort is used and is set to the ascending order of the PrimaryKey property of the underlying DataTable (if the PrimaryKey is set). The default is false. |
Count | int | Gets the number of rows visible to your DataView. |
DataViewManager | DataViewManager | Gets the DataViewManager associated with your DataView. You'll learn about DataViewManager objects later in the section "Creating and Using a DataViewManager Object." |
RowFilter | string | Gets or sets the expression used to filter rows in your DataView. |
RowStateFilter | DataViewRowState | Gets or sets the expression used to filter rows based on constants from the DataViewRowState enumeration. Values are shown in Table 13.3. |
Sort | string | Gets or sets an expression that specifies the columns to sort by and optional sort order for the rows in your DataView. This string expression contains the column name followed by ASC (for ascending sort) or DESC (for descending sort). A column is sorted in ascending order by default. You separate multiple columns by commas in the string. For example: CustomerID ASC, CompanyName DESC. |
Table | DataTable | Gets or sets the underlying DataTable that your DataView is associated with. |
METHOD | RETURN TYPE | DESCRIPTION |
---|---|---|
AddNew() | DataViewRow | Adds a new DataRowView to your DataView, and therefore adds a new DataRow to the underlying DataTable. |
BeginInit() | void | Begins the runtime initialization of your DataView in a form or component. |
CopyTo() | void | Copies the rows from your DataView into an array. This method is for Web Forms Interfaces only. |
Delete() | void | Deletes the DataRowView at the specified index from your DataView. The deletion of the underlying DataRow isn't permanent until you call the AcceptChanges() method of your DataTable. You can undo the delete by calling the RejectChanges() method of your DataTable. |
EndInit() | void | Ends the runtime initialization of your DataView in a form or component. |
Find() | int | Overloaded. Finds and returns the index of the DataRowView with the specified primary key in your DataView. The int returned by this method is the index of the DataRowView if found; otherwise -1 is returned. You must first set the Sort property of your DataView to sort on the primary key. For example, if you want to find a DataRowView based on the CustomerID, you must set Sort to CustomerID, CustomerID ASC, or CustomerID DESC. |
FindRows() | DataRowView[] | Overloaded. Finds and returns an array of DataRowView objects that have columns matching the specified primary key. As with the Find() method, you must set the Sort property of your DataView to sort on the primary key before calling the FindRows() method. |
GetEnumerator() | IEnumerator | Returns an enumerator for your DataView. |
ToString() | string | Returns a string that represents your DataView. |
CONSTANT | DESCRIPTION |
---|---|
Added | A new row. |
CurrentRows | The current rows, which include Unchanged, Added, and ModifiedCurrent rows. |
Deleted | A deleted row. |
ModifiedCurrent | A current row that has been modified. |
ModifiedOriginal | The original row before it was modified. |
None | Doesn't match any of the rows in the DataTable. |
OriginalRows | The original rows, which include Unchanged and Deleted rows. |
Unchanged | A row that hasn't been changed. |
One of the DataView events is ListChanged. It fires when the list managed by your DataView changes. Its event handler is ListChangedEventHandler.
Table 13.3 shows the members of the System.Data.DataViewRowState enumeration. This enumeration is used with the RowState property of a DataTable; this property is used to specify that the rows viewed by the DataView are filtered by their DataViewRowState.
|
|