How to add an autonumber column in a DataGridView control that is populated with a DataTable in simple steps ?
Figure.1
The first column in Figure.1 displays sequential row numbers. I have achieved this output by using the following function.
private DataTable AutoNumberedTable(DataTable SourceTable)
{
DataTable ResultTable = new DataTable();
DataColumn AutoNumberColumn = new DataColumn();
AutoNumberColumn.ColumnName=
"S.No.";
AutoNumberColumn.DataType =
typeof(int);
AutoNumberColumn.AutoIncrement =
true;
AutoNumberColumn.AutoIncrementSeed = 1;
AutoNumberColumn.AutoIncrementStep = 1;
ResultTable.Columns.Add(AutoNumberColumn);
ResultTable.Merge(SourceTable);
return ResultTable;
}