简体   繁体   English

如何制作C#DataTable过滤器

[英]How to make C# DataTable filter

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 ? 我想要行“ ID = 1和ID2 = 3”如何制作?

你是这个意思吗?

dtData.Select("ID=1 AND ID2=3");

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 注意,在我的Select()中,我将条件放在“ 与”和“ 或”之间的Parens中

Hope this helps! 希望这可以帮助! Mike V 迈克五世

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();
DataTable dt2 = dt.Select("ID = 1").CopyToDataTable;

确保dt中有行

在这里,您可以使用Linq的CopyToDataTable方法将内容复制到另一个DataTable,同时通过过滤选择特定的行。

DataTable dt2 = dt.Select("state = 'FL' ").CopyToDataTable; 

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' "); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM