How to make C# DataTable filter

2020-01-29 08:25发布

my datatable;

    dtData

    ID | ID2
    --------
    1  |  2
    1  |  3


dtData.Select("ID = 1"); one more rows;

i want row "ID = 1 And ID2 = 3" how to make ?

标签: c# datatable
6条回答
够拽才男人
2楼-- · 2020-01-29 09:00

Great example and very helpful. Wanted to add one thing - if you need to select on a string use something like:

DataTable dt2 = dt.Select("state = 'FL' "); 
查看更多
放荡不羁爱自由
3楼-- · 2020-01-29 09:06

Okay, here is how I do such things...

    GridFieldDAO dao = new GridFieldDAO();
    //Load My DataTable
    DataTable dt = dao.getDT();
    //Get My rows based off selection criteria
    DataRow[] drs = dt.Select("(detailID = 1) AND (detailTypeID = 2)");
    //make a new "results" datatable via clone to keep structure
    DataTable dt2 = dt.Clone();
    //Import the Rows
    foreach (DataRow d in drs)
    {
        dt2.ImportRow(d);
    }
    //Bind to my new DataTable and it will only show rows based off selection 
    //criteria
    myGrid.DataSource = dt2;
    myGrid.DataBind();

Notice in my Select() I put the criteria in Parens between AND and OR

Hope this helps! Mike V

查看更多
我命由我不由天
4楼-- · 2020-01-29 09:10

Do you mean like this?:

dtData.Select("ID=1 AND ID2=3");
查看更多
甜甜的少女心
5楼-- · 2020-01-29 09:12

Better use this :

GridFieldDAO dao = new GridFieldDAO();
//Load My DataTable
DataTable dt = dao.getDT();
//Get My rows based off selection criteria and copy them directly to datatable
DataTable dt2 = dt.Select("(detailID = 1) AND (detailTypeID = 2)").CopyToDataTable();
查看更多
手持菜刀,她持情操
6楼-- · 2020-01-29 09:25

Here you can copy your contents to another DataTable by using CopyToDataTable method of Linq while selecting the specific rows by filtering.

DataTable dt2 = dt.Select("state = 'FL' ").CopyToDataTable; 
查看更多
冷血范
7楼-- · 2020-01-29 09:27
DataTable dt2 = dt.Select("ID = 1").CopyToDataTable;

make sure dt has rows in it

查看更多
登录 后发表回答