blah blah blah is here! blah blah » Close

up1down
link

Guys, I want to count the rows on my datagrid but counting -1.

for example if i had 1 row in my datagrid i want label1 to display 0

I'm using the code below, it counts correct but i cant adapt it to count -1? am I missing something simple?

int count = 0; foreach (DataGridViewRow row in vehDataGridView.Rows)
{
if (row.IsNewRow) break;
object obj = row.Cells;
if (obj != null && !DBNull.Value.Equals(obj) )count++;
{
label1.Text = count.ToString();
}
}

last answered one year ago

1 answers

link

Well, the number of rows in the DGV excluding the 'new row' at the bottom is simply vehDataGridView.Rows.Count -1.

So this should work:

int count = vehDataGridView.Rows.Count -1;
if (count > 0)
{
label1.Text = (count - 1).ToString(); // counting from 0
}

Nice one vulpes, just how i wanted it to work.

Feedback