Andy
Andy

Reputation: 49

Set datagridview cell value to index number of row c#

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

Answers (1)

Pvria Ansari
Pvria Ansari

Reputation: 456

Try this:

    foreach (var row in dgv.Rows.Cast<DataGridViewRow>())
    {
        dgv[0, row.Index].Value = row.Index;
    }

Upvotes: 2

Related Questions