blah blah blah is here! blah blah » Close

up1down
link

Guys, on form load i count my datagridview rows that only contain the status "UPREPD USED" and display the total on a label, it works fine.

My problem is i now want to count the rows that contain the status "UPREPD USED" and "UPREPD NEW" the code that works for the top line is below, i've tried putting ++ between the text quotations but doest work.

int count = 0; foreach (DataGridViewRow row in usedDataGridView.Rows)
{
if (row.IsNewRow) break;
object obj = row.Cells[2].Value;
if (obj != null && !DBNull.Value.Equals(obj) && obj.ToString() == "UNPREPD USED") count++;
{
label6.Text = count.ToString();
}
}


As mentioned the above works but only to count one status.

last answered one year ago

1 answers

link

Just add to the condition a check for "UNPREPD NEW".

int count = 0;
foreach (DataGridViewRow row in usedDataGridView.Rows)
{
if (row.IsNewRow) break;
object val = row.Cells[2].Value;
if (val != null && !DBNull.Value.Equals(val) &&
(val.ToString() == "UNPREPD USED" || val.ToString() == "UNPREPD NEW"))
count++;
}
label6.Text = count.ToString();


[edit]
oops on varnames.

Great reply - works a treat. Thanks.

Feedback