M
M
Mykola Franchuk2016-09-06 19:53:45
C++ / C#
Mykola Franchuk, 2016-09-06 19:53:45

How to number rows in DataTable as source for DataGridView?

Hello.
You need to number the rows in the DataGridView , but because storing data in the control is a bad idea, it was decided to use the DataTable instance for the DataGridView as the data source (DataSource property).

private DataTable table;
//.......
table = new DataTable();
DataGridView.DataSource = table;

But if the numbering of the columns is supported by both the DataGridView and the DataTable and there are no problems with this, then I managed to find the numbering of the lines only in the DataGridView. You need to do the usual line numbering as in Excel (1, 2, 3, ...). I did not find a similar code for DataTable:
rows = DataGridView.RowCount;
for (int i = 0; i < rows; i++)
{
       DataGridView.Rows[i].HeaderCell.Value = (i + 1).ToString();   // i+1 потому, что нумерация нужна 
}                                              // с еденицы, а не с нуля

Does DataTable really not support line numbering? If it does, how do you do it? If it doesn't, what is the best data source for the DataGridView to use?
PS When using the DataTable as a data source, for some reason it is impossible to number the DataGridView directly, as in the code above. Although without using a DataTable, the line numbering works fine. There is no error, no warning, the code simply does not lead to anything, as if it did not number anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-09-06
@kuzia_bRatok

Read more carefully where it says Autoincrement
MSDN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question