views:

364

answers:

2

If I have a DataTable and want to create a new row I first call DataTable.NewRow() which returns a DataRow. This row then has the schema of the Table, and I can set the value of each field in the row. At this point does the Table "know" about the Row, or does that only happen after I call DataTable.Rows.Add(DataRow row)?

If I call D**ataTable.Rows.Add()** more then once are duplicate rows created in the data table with duplicate values?

The reason I ask is I'm trying to write a recursive function that will itterate down a tree structure and populate a DataTable with values. So I have situations where rows being created for each child will have columns that have all the same values because they come from the parent.

So my idea was to pass a DataRow with the parent's information to each child. The child would then add it's information and then add the row to the table. But I'm trying to figure out if I need to call DataTable.NewRow() for each child, or if new rows will be created automaticly if I add the same row multiple times.

A: 

No, it will generate an exception of type System.ArgumentException.

rahul
Is there an easy way of creating a Duplicate DataRow with the same values?
Eric Anastas
+3  A: 

No, but it's easy to copy a row so you can get what you want (assumes the existing row is in a variable called existingRow):

var newRow = dataTable.NewRow();
newRow.ItemArray = existingRow.ItemArray;
dataTable.Rows.Add(newRow);
free-dom