tailieunhanh - Finding Rows in a DataView

[ Team LiB ] Recipe Finding Rows in a DataView Problem You need to find a row or group of rows in a DataView meeting certain criteria. Solution Use a sorted DataView to find rows using columns that are not part of the primary key. The sample code contains two event handlers: Sets up the sample by creating a DataTable containing the Orders table from the Northwind database. A DataView of the table sorted by the CustomerID and EmployeeID is created. Find Uses the FindRows( ) method of the DataView to retrieve the array of DataRowView objects. | Team LiB Recipe Finding Rows in a DataView Problem You need to find a row or group of rows in a DataView meeting certain criteria. Solution Use a sorted DataView to find rows using columns that are not part of the primary key. The sample code contains two event handlers Sets up the sample by creating a DataTable containing the Orders table from the Northwind database. A DataView of the table sorted by the CustomerlD and EmployeelD is created. Find Uses the FindRows method of the DataView to retrieve the array of DataRowView objects matching the CustomerlD and OrderlD specified. The code iterates over the collection to return the results. The C code is shown in Example 3-9. Example 3-9. File Namespaces variables and constants using System using using using using private DataView dv . . . private void DataViewSearchPerformanceForm_Load object sender e Fill the source table with schema and data. SqlDataAdapter da new SqlDataAdapter SELECT FROM Orders S ql_ConnectString DataTable dt new DataTable Orders dt dt Create the data view for the Orders table and sort. dv new DataView dt CustomerlD EmployeelD private void findButton_Click object sender e StringBuilder result new StringBuilder DataRowView foundRows Find the rows by the sort key value ProductID. try foundRows new object catch FormatException ex return Display the results. if 0 No rows found. else ORDER tREQUIRED DATE Iterate over the collection of found rows. foreach DataRowView row in foundRows row OrderID t row RequiredDate COUNT t .