Datatable filters datarow that meets the criteria
Datatable filters row. In. Net framework3.5 and 4.0, it is relatively easy to use because of the existence of LINQ. In framework2.0, it is a little clumsy, but it is also very useful. Is to use the datatable. Select () method, the specific definition is as follows: Public datarow [] Select (string filterexpression, string sort) Ex: datarow [] rows = DT. Select ("name like '% Jake %'", "Age DESC "); After filtering a travel set, you need to import the row set to a new able. The procedure is as follows: 1. datatable dtnew = DT. Clone (); the new dtnew and DT have the same architecture and constraints. 2. note that you cannot add a new rows set to dtnew using the foreach or for loop method. An error is reported, indicating that the row belongs to another datatable, you can use another method. For (INT I = 0; I <rows. length; I ++) { Dtnew. importrow (rows [I]); } When you query msdn, you will find the definition of this method: Copy datarow to the datatable, and retain any attribute settings, initial values, and current values. Note: When newrow is called, a row is added to the table using the existing table architecture, the default value is filled for the row, and the datarowstate is set to added. Calling importrow will retain the existing datarowstate and other values in the row. |
-------------------------------------------------- Msdn -----------------------------------------------
Private void getrowsbyfilter ()
{
Datatable table = dataset1.tables ["orders"];
// Presuming the datatable has a column named date.
String expression;
Expression = "date >#1/1/00 #";
Datarow [] foundrows;
// Use the select method to find all rows matching the filter.
Foundrows = table. Select (expression );
// Print column 0 of each returned row.
For (INT I = 0; I <foundrows. length; I ++)
{
Console. writeline (foundrows [I] [0]);
}
}
Select () gets the array of all datarow objects.
Select (string) obtains an array of all datarow objects that match the filtering conditions according to the primary key order (if there is no primary key, the order is added.
Select (string, string) gets an array of all datarow objects in the specified sorting order that matches the filtering conditions.
Select (string, String, dataviewrowstate) to obtain the array of all datarow objects that match the filter in the sorting order and the specified status.