Reputation: 445
Is there any way to do an increment number from 1,2,3 and so on for my column in my datatable?
For example similar to column "Code" below
or at least is there any other way that i can name the numbers manually?
Here's what I've tried (but not working)
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
Thanks for your reply
Upvotes: 0
Views: 243
Reputation: 27367
AutoIncrement
is indeed the right approach here. You might be missing something else, but the following code does work:
var dt = new DataTable();
dt.Columns.Add(new DataColumn { ColumnName = "Code", AutoIncrement = true, DataType = typeof(int) });
dt.Columns.Add(new DataColumn { ColumnName = "Resource Type", DataType = typeof(string) });
dt.Columns.Add(new DataColumn { ColumnName = "Number of hits", DataType = typeof(int) });
var newRow = dt.NewRow();
newRow[1] = "Testing";
newRow[2] = 3;
dt.Rows.Add(newRow);
var newRow2 = dt.NewRow();
newRow2[1] = "Testing 2";
newRow2[2] = 6;
dt.Rows.Add(newRow2);
And when inspecting dt
, the result is:
Code Resource Type Number of hits 0 Testing 3 1 Testing 2 6
Upvotes: 1