2017-02-17 29 views
2

我有一個數據表,它將包含來自不同用戶的信息,每個數據的用戶都作爲列存儲在我的數據表中。使用條件c從數據表中提取數據#

我如何才能提取相關數據,以顯示在datagridview只有當用戶= msalmon,而不是像約翰?

我的表: My table data

+1

'VAR的查詢= table.AsEnumerable(),其中(R => r.Field ( 「SessionUName」)= =「msalmon」);' –

+0

當我用我的數據表名替換「table」時,我在「AsEnumerable」下得到了一個錯誤:/ –

+0

您需要導入System.Linq並添加一個對System.Data.DataSetExtensions的引用'dll。 [閱讀](http://stackoverflow.com/a/28267846/284240) –

回答

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

這是一個非常大的幫助,謝謝! –