Michael Michael - 4 months ago 58
C# Question

Filling datatable with int array.

I have this array of integers

int[] arr = [2,3,54,6];

I have this dataTable:

var myDataTable = new DataTable();

How can I fill myDataTable with arr?

myDataTable.Columns.Add("Numbers", typeof(int));
foreach(int num in arr)

this will add a column and populate it with the numbers, for multiple columns your row insert would instead be myDataTable.Rows.Add(num, secondCol, thirdCol, etc...);

If you want to clear the data before each insert, you can also call myDataTable.Clear(); before inserting anything