Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding empty rows to a DataTable

Tags:

c#

.net

datatable

Is it possible to add a few rows to a DataTable with a single call?

The issue is that I need to create a DataTable where each column requires a previous processing before being written into the DataTable. Writing row by row would be inconvenient.

I.e., I need to create a DataTable with N empty rows and later write values in a column basis.

like image 785
sapito Avatar asked Oct 30 '14 13:10

sapito


People also ask

How to add new empty row DataTable in c#?

create temporary table (TMP) with the only columns consisting PrimaryKey matching DST. PriaryKey (without any rows yett) merge data from DST to TMP - this will create just rows. manually add columns matching SRC - this will create "empty" rows.

How do I add a row to a data table in R?

To add or insert observation/row to an existing Data Frame in R, we use rbind() function. We can add single or multiple observations/rows to a Data Frame in R using rbind() function. The basic syntax of rbind() is as shown below.


2 Answers

You cannot add multiple rows without loops. If you want to add n rows to a DataTable where all columns are "empty" use DataRowCollection.Add without arguments:

for(int i = 0; i < n; i++)
    table.Rows.Add();  // all fields get their default value
like image 120
Tim Schmelter Avatar answered Oct 16 '22 08:10

Tim Schmelter


in your for loop, (obviously needs a forloop to add multiple rows), you want to use

for(int i = 0; i < n; i++)
    table.Rows.Add();   

if you know what you want to put in the row, you can tweak this code:

DataRow newBlankRow = theDataTable.NewRow();
theDataTable.Rows.InsertAt(newBlankRow, theDataTable.Rows.Count);
like image 4
Max Alexander Hanna Avatar answered Oct 16 '22 08:10

Max Alexander Hanna