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!
No comments:
Post a Comment