DataView Class
The DataView class in ADO.NET provides a way to create a custom view of a DataTable. It allows you to filter, sort, and navigate through the rows of a DataTable without modifying the original data. This is particularly useful for data-bound controls that need to display specific subsets or sorted orders of data.
Overview
A DataView is a dynamic view of the data contained in a DataTable. Changes made to the underlying DataTable are immediately reflected in the DataView, and vice-versa, allowing for real-time data manipulation.
Key features of DataView include:
- Filtering: You can filter the rows based on specified criteria.
- Sorting: You can sort the rows by one or more columns.
- Row State Filtering: You can choose to view only current, original, or deleted rows.
- Data Binding: Easily used with data-bound controls like DataGridView, ListBox, etc.
Creating a DataView
To create a DataView, you instantiate the DataView class, passing the DataTable you want to view as a constructor argument.
// Assume 'myDataTable' is an existing DataTable object
DataView myDataView = new DataView(myDataTable);
Filtering Data
The RowFilter property is used to specify a filter expression. The expression syntax is similar to the `WHERE` clause in SQL.
// Filter to show only rows where 'City' is 'London'
myDataView.RowFilter = "City = 'London'";
// Filter to show rows where 'Age' is greater than 30
myDataView.RowFilter = "Age > 30";
// Filter with multiple conditions
myDataView.RowFilter = "Country = 'USA' AND Age > 25";
Sorting Data
The Sort property is used to specify the sorting order. The expression syntax is similar to the `ORDER BY` clause in SQL.
// Sort by 'LastName' in ascending order
myDataView.Sort = "LastName ASC";
// Sort by 'Age' in descending order
myDataView.Sort = "Age DESC";
// Sort by multiple columns
myDataView.Sort = "Country ASC, City DESC";
Row State Filtering
The RowStateFilter property allows you to specify which versions of rows to display.
DataViewRowState.CurrentRows: Shows the current state of rows (default).DataViewRowState.OriginalRows: Shows the original state of rows before any modifications.DataViewRowState.Deleted: Shows rows that have been deleted.DataViewRowState.Added: Shows rows that have been added.DataViewRowState.ModifiedCurrent: Shows modified rows in their current state.DataViewRowState.ModifiedOriginal: Shows modified rows in their original state.
// Show only the original rows
myDataView.RowStateFilter = DataViewRowState.OriginalRows;
Data Binding Example
Here's a simple example of how you might bind a DataView to a DataGridView in a Windows Forms application.
// Assuming you have a DataGridView named 'dataGridView1'
// and a DataTable named 'customersTable' populated with data
DataView customerView = new DataView(customersTable);
customerView.RowFilter = "Country = 'UK'";
customerView.Sort = "CompanyName ASC";
dataGridView1.DataSource = customerView;
Properties Summary
| Property | Description |
|---|---|
AllowDelete |
Gets or sets a value indicating whether rows can be deleted through the DataView. |
AllowNew |
Gets or sets a value indicating whether new rows can be added through the DataView. |
AllowEdit |
Gets or sets a value indicating whether rows can be edited through the DataView. |
Table |
Gets the DataTable associated with this DataView. |
RowFilter |
Gets or sets the filter expression used to filter the rows. |
Sort |
Gets or sets the sort expression used to sort the rows. |
RowStateFilter |
Gets or sets the filter for row states. |
Count |
Gets the number of rows in the DataView after filtering and sorting. |
Note: When modifying data through a DataView, be mindful of the AllowEdit, AllowNew, and AllowDelete properties. Ensure they are set appropriately for your application's requirements.