Rose Donkelaar
Rose Donkelaar

Reputation: 73

Extracting data from datatable with conditions c#

I have a datatable which will contain information from various users, the user for each data is stored as a column in my datatable.

How can I extract the relevant data to show in a datagridview only when the user = msalmon and not something like John?

My table: My table data

Upvotes: 2

Views: 1257

Answers (1)

NicoRiff
NicoRiff

Reputation: 4883

You can do the following:

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]);
    }
}

Upvotes: 2

Related Questions