2016-05-17 74 views
0

我正在嘗試對特定列上的行進行計數。我在我的數據網格表上有這些數據。C#如何統計數據網格表上特定列的行數

日期---------- ------------ TimeIn超時

05/16/2016 --- 08:00:00 AM --  05:00:00 PM 

05/16/2016 --- 08:00:00 AM -- 

我用

lblCount.Text = DataGridTable1.RowCount.ToString(); 

和輸出是2.

我如何計算列TimeOut的行數?就像這樣,如果我對TimeOut列沒有任何價值,那麼這行不會被計算在內。

所以輸出將是1.

欣賞幫助。謝謝。

回答

1

一種解決方案可能是計數非空的單元格循環通過列值,我們可以使用如下的Linq來執行此操作。

int count = dataGridView1.Rows 
    .Cast<DataGridViewRow>() 
    .Select(row => (string)row.Cells["TimeOut"].Value) 
    .Where(v => !string.IsNullOrEmpty(v)) 
    .Count(); 
+0

我一直在尋找這個答案,對於一些是時候了。所以我做了什麼,我把它轉換成了字符串,這樣我的標籤就可以讀取它。非常感謝。非常感謝您的幫助。 –

0

你可能必須通過所有的行進行迭代,並計算你所需的列的列

像如下:

foreach (DataGridViewRow rw in this.dataGridView1.Rows) 
    { 

     if (rw.Cells["TimeOut"].Value == null || rw.Cells["TimeOut"].Value == DBNull.Value || String.IsNullOrWhitespace(rw.Cells["TimeOut"].Value.ToString()) 
     { 
     // here increase your counter 
     } 

    } 

ref link

相關問題