Working with Data—Filtering the Data with the DataView
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| Database Articles |
|
© 2006 Jeffery Suddeth |
Filtering the Data with the DataView
The DataView class can be used to create a new view of the table that can be filtered or sorted without affecting the underlying table. The DataView is similar to a table in that it has a collection of rows but the objects in the DataView’s Rows collection are of type DataRowView. You use the DataRowView the same as you would use a DataRow. You use the name of the field as an index to get the value from the column. Once you have a DataView, you can filter or sort the data without affecting the actual table.
You create a DataView by passing a DataTable to the DataView constructor.
// create a view that can be filtered and sorted DataView view = new DataView(ds.Tables[0]); foreach (DataRowView drv in view) { Console.WriteLine(drv["Name"].ToString()); } // find the record for Rick view.RowFilter = "Name = 'Evy'"; foreach (DataRowView drv in view) { Console.WriteLine(drv["Name"].ToString()); }
In the code segment, we create a DataView for the first DataTable in the DataSet’s Tables collection. Then, we loop through the Rows collection, printing out the Name field as we go. Then, we filter the view so that view only contains records where the Name field is "Evy". The foreach loop iterates through each row in the view of "Evy" records and prints out the Name field.
Table 15.5 – OLEDB Database Classes
| Class | Description |
OleDbConnection
| The physical database connection |
OleDbCommand
| A command to execute on the database |
OleDbDataAdapter
| Moves data between the DataSet and the physical Database using a connection and a command |
OleDbDataReader
| Can iterate results returned from a command |
Now that you have been introduced to the ADO.NET classes, it is time to write some real code. The example in the next section connects to the Microsoft Access Northwind Traders database using the OleDb classes.
|

