简体   繁体   中英

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;
        }

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM