从条件c#中的数据表中提取数据

时间:2017-02-17 11:43:47

标签: c# datatable conditional-statements

我有一个数据表,其中包含来自不同用户的信息,每个数据的用户都存储为我的数据表中的一列。

如果仅在user = msalmon而不是像John这样的东西时,如何提取要在datagridview中显示的相关数据?

我的桌子: My table data

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

private void GetRowsByFilter()
{
    DataTable yourDataTable = new DataTable(); //Your DataTable is supposed to have the data
    // Presuming the DataTable has a column named user.
    string expression;
    expression = "user = \"msalmon\"";
    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]);
    }
}