0

I'm trying to count row on a specific column. I have this data on my data grid table.

Date ---------- TimeIn ------------TimeOut

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

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

I used

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

and the output is 2.

How can i count the number of row of the column TimeOut? It's like that if I don't have any value on column TimeOut the row will not be counted.

So the output will be 1.

Appreciate the help. Thanks.

Manish Jesani
  • 1,339
  • 5
  • 20
  • 36

2 Answers2

1

One solution could be counting non empty cells looping through column values, we could do this using Linq as below.

int count = dataGridView1.Rows
    .Cast<DataGridViewRow>()
    .Select(row => (string)row.Cells["TimeOut"].Value)
    .Where(v => !string.IsNullOrEmpty(v)) 
    .Count();
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • I've been looking for this answers for some time now. So what I did, I converted it into string so my label can read it. Thanks a lot. Really appreciate your help. – neiljon dela cruz May 17 '16 at 04:43
0

you may have to iterate through all the rows and count your desired column's row

like follow :

    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

Community
  • 1
  • 1
Amit
  • 1,821
  • 1
  • 17
  • 30