An array of C#, PHP, and HTML programming articles, tutorials, and resources

Posts Tagged ‘ datarow ’

At some point, every C# programmer deals with populating, selecting, and even deleting from a DataTable. A moderately advanced operation is transferring an entire single row from an original DataTable and inserting it into a new DataTable. My original thoughtprocess is the following:

DataTable originalTable = new DataTable();
originalTable.Columns.Add("id", typeof(int));
originalTable.Columns.Add("firstName", typeof(string));
originalTable.Columns.Add("lastName", typeof(string));
originalTable.Columns.Add("age", typeof(string));
originalTable.Columns.Add("gender", typeof(string));

DataRow dr = originalTable.NewRow();
dr["id"] = 1;
dr["firstName"] = "Victor";
dr["lastName"] = "Chen";
dr["age"] = 5;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 2;
dr["firstName"] = "John";
dr["lastName"] = "Doe";
dr["age"] = 35;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 3;
dr["firstName"] = "Jane";
dr["lastName"] = "Doe";
dr["age"] = 32;
dr["gender"] = "F";
originalTable.Rows.Add(dr);

DataTable newTable = originalTable.Clone();
newTable.Rows.Add(originalTable.Rows[1]);

Though the above example is long, lines 1 - 30 handle creating and populating the original DataTable (If you need explanation of how this works, check out Add Rows to a DataTable in C# tutorial). Line 32 creates a new DataTable by taking the structure of the original DataTable. In Line 33, we attempt to select Row 1 from the original DataTable and add it as a new row in the new DataTable. However, the above example above does not work. It will throw the runtime error below.

This row already belongs to another table.

The correct solution takes more lines of coding (and therefore more logic), but ultimately gets the job done. It involves using a DataRow’s ItemArray method to transfer a row’s data column by column through an object array, and inserting them column by column back into a new DataRow. See the example below:

DataTable originalTable = new DataTable();
originalTable.Columns.Add("id", typeof(int));
originalTable.Columns.Add("firstName", typeof(string));
originalTable.Columns.Add("lastName", typeof(string));
originalTable.Columns.Add("age", typeof(string));
originalTable.Columns.Add("gender", typeof(string));

DataRow dr = originalTable.NewRow();
dr["id"] = 1;
dr["firstName"] = "Victor";
dr["lastName"] = "Chen";
dr["age"] = 5;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 2;
dr["firstName"] = "John";
dr["lastName"] = "Doe";
dr["age"] = 35;
dr["gender"] = "M";
originalTable.Rows.Add(dr);

dr = originalTable.NewRow();
dr["id"] = 3;
dr["firstName"] = "Jane";
dr["lastName"] = "Doe";
dr["age"] = 32;
dr["gender"] = "F";
originalTable.Rows.Add(dr);

DataTable newTable = originalTable.Clone();
DataRow newRow = newTable.NewRow();
newRow.ItemArray = originalTable.Rows[1].ItemArray;
newTable.Rows.Add(newRow);

Keeping consistent with our original example, lines 1-30 handle creating a DataTable. On line 32, create a DataTable. On Line 32, create a new DataRow. On Line 33, I select Row 1 from the Original DataTable, take it’s ItemArray, and transfer it to the newly created DataRow from Line 32. Finally, on line 34, I transfer newRow into the newTable.

If you have more than one row you would like to copy, merely repeat this process through a loop. Happy Coding!

When using a DataTable as a DataSource to a GridView in C#, it is often necessary to add additional rows, whether they be blank rows or fully populated rows, to the control. It has been my experience that it is much easier to add a DataRow into the DataTable prior to a DataBind.

The first step is to create an empty datatable. It would be perfectly legal to start with a datatable prepopulated with data. In this case, we are naming the Datatable, dt.

DataTable dt = new DataTable();

The next step is to create a new DataRow object. In this case, we are calling it “newRow”. The next step would be to initialize the DataRow object, which is the second line below:

DataRow myNewRow;
myNewRow = dt.NewRow();

Now that the DataRow has been created and initialized, simply add values to the object as you would an array. In the two examples below, we are adding values to two different columns in our row. We are first assigning the column with name customerId the integer value of 1. Secondly, we are assigning the column with name username a string johndoe. Repeat as needed.

myNewRow["customerId"] = 1;
myNewRow["username"] = "johndoe" ;

Finally, we want to add this newly created row (with two columns) to the originally blank datatable we created in the beginning.

dt.Rows.Add(myNewRow);

All the steps from above are put together in the example below:

DataTable dt = new DataTable();
DataRow myNewRow;
myNewRow = dt.NewRow();
myNewRow["customerId"] = 1;
myNewRow["username"] = "johndoe" ;
dt.Rows.Add(myNewRow);

With that you can now set this datatable as a DataSource and DataBind a GridView or any other similar C# control.