Datatable filters datarow that meets the criteria

Source: Internet
Author: User


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.