Michael Michael - 1 month ago 17
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
myDataTable.Columns.Add("Numbers", typeof(int));
foreach(int num in arr)
{
    myDataTable.Rows.Add(num);
}

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

Comments