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.

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.