Hi,
Is there a simple way to add numbers in a column in a datatable, starting at 0 or 1, and then increment by 1 for each row that holds data?
So if there are 5 rows in the dt, a new column should show
1
2
3
4
5
Hi,
Is there a simple way to add numbers in a column in a datatable, starting at 0 or 1, and then increment by 1 for each row that holds data?
So if there are 5 rows in the dt, a new column should show
1
2
3
4
5
You want to create a datatable with one column and the number of rows you specified with a sequence of values
You can sue generate datatable with the following
"Column1" + Environment.NewLine + String.Join(Environment.NewLine,Enumerable.Range(1,varcount).ToArray())
And select newline as the row separator…so this creates a table with columnname as column 1 and will add rows from 1 to varcount…var count being a variable to which you can send the input number if you send 5 it adds 5 rows from 1to 5
cheers
Please find the following resource in which your requirements are met:
RowDataProcess.zip (147.4 KB)
Input:
Output:
Hope this helps,
Best Regards.
How about this expression?
dataTable.Columns.Add("ID", GetType(Integer))
For i As Integer = 0 To dataTable.Rows.Count - 1
dataTable.Rows(i).Item("ID") = i + 1
Next
This will add a new column named “ID” to your DataTable and fill it with numbers starting from 1 .
You can also use an Auto-Number System column to generate an automatically incremented counter for each row that contains data
Check out the thread
Regards
Gokul
Thank you very much!
I thing you are using ‘Index’ with a caps I. can you check with ‘index’ as I declared a variable with small i?
Best Regards.
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.