Get the index while looping
Is it possible to get the current index while looping?
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue 开发者_开发百科= DGV.CurrentRow.Index + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
But I have in output :
0 First
0 Second
0 ...
I want to get :
1 First
2 Second
3 ...
Thanks.
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = (i + 1).ToString() + " " + DGV.Rows[i].Cells[1].Value.ToString();
}
BTW: I'd prefer:
for (int i = 0; i < DGV.Rows.Count - 2; i++)
{
myValue = String.Format("{0} {1}", i + 1, DGV.Rows[i].Cells[1].Value);
}
It's the index just i+1
in this case?
Try:
myValue = (i+1) + " " + DGV.Rows[i].Cells[1].Value.ToString();
you can do this way also
for (int i = 1; i < DGV.Rows.Count - 1; i++)
{
myValue = i.ToString() + " " + DGV.Rows[i-1].Cells[1].Value.ToString();
}
精彩评论