如何制作C#DataTable过滤器

时间:2010-07-29 12:08:36

标签: c# datatable

我的数据表;

    dtData

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


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

我想要行“ID = 1和ID2 = 3”如何制作?

6 个答案:

答案 0 :(得分:29)

你的意思是这样吗?:

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

答案 1 :(得分:12)

好的,这就是我做这些事情的方式......

    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();

请注意我的Select()我将Parens中的标准放在 AND OR

之间

希望这有帮助! Mike V

答案 2 :(得分:6)

更好地利用这个:

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();

答案 3 :(得分:2)

DataTable dt2 = dt.Select("ID = 1").CopyToDataTable;

确保dt中有行

答案 4 :(得分:1)

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

答案 5 :(得分:0)

很好的例子,非常有帮助。想添加一件事 - 如果你需要在字符串上选择使用类似的东西:

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