Michael Michael - 1 year ago 115
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?

Answer Source
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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download