for (int i = 2; i <= SCdataGridView1.Rows.Count;i++)
{
int malecount = 0;
if (SCdataGridView1.Rows[i].Cells[8].Value.ToString() == "Male") //here the error occure because I put here i
{
malecount = malecount + 1;
SClabel6.Text = malecount.ToString();
}
}
error is
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
What is my mistake?
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Kirtan PatelRank #292 · 8.6K reputationPosted Oct 24, 2009, 3:53 AM EDT
for (int i = 2; i <= SCdataGridView1.Rows.Count-1;i++)
{
int malecount = 0;
if (SCdataGridView1.Rows[i].Cells[8].Value.ToString() == "Male")
{
malecount = malecount + 1;
SClabel6.Text = malecount.ToString();
}
}
or you can also write i
jingeRank #2,527 · 554 reputationPosted Oct 24, 2009, 3:54 AM EDT
for (int i = 2; i < SCdataGridView1.Rows.Count;i++) { int malecount = 0; if (SCdataGridView1.Rows[i].Cells[8].Value.ToString() == "Male") //here the error occure because I put here i { malecount = malecount + 1; SClabel6.Text = malecount.ToString(); } }Kirtan PatelRank #292 · 8.6K reputationPosted Oct 24, 2009, 4:55 AM EDT
what happened to you why you unchecked my answer" ?? :( does it not helped you ????