I need to show an auto increment value in cells of a column in DataGridView. The type of column is DataGridViewLinkColumn and the grid should be like this:
| Column X | Column Y |
-----------------------
| 1 | ........ |
| 2 | ........ |
| ........ | ........ |
| n | ........ |
I tried these codes, but it doesn't work:
int i = 1;
foreach (DataGridViewLinkColumn row in dataGridView.Columns)
{
row.Text = i.ToString();
i++;
}
Can anyone help me, please?
You can handle
CellFormattingevent of yourDataGridViewand then provide the value for cell there:It's better to not use a simple
fororforeachloop because if you sort the grid using another column, the order of numbers in this column will be unordered.