Reputation: 49
I would like my code to loop through all rows in my datagridview and set the first column value in each row to the row index number of itself. My code so far does not work. Any help appreciated.
if (e.RowIndex >= 0)
{
this.DataGridView.Rows(e.RowIndex).Cells(0).Value = e.RowIndex + 1;
}
Upvotes: 1
Views: 1035
Reputation: 456
Try this:
foreach (var row in dgv.Rows.Cast<DataGridViewRow>())
{
dgv[0, row.Index].Value = row.Index;
}
Upvotes: 2