1

I want to do this:

    public void UpdateDataRowsToDataTable(string tableName, List<DataRow> rows)
    {
        foreach (DataRow row in rows)
        {
            // this method does not exist!!
            _dataSet.Tables[tableName].Rows.Update(row);
        }
    }

I need a method that finds a row (maybe by primary key) and updates changed rows to the row in DataTable.

The only possibility what I know is to use Select (or Find) and then maybe iterate all columns and give them new values. Please tell me that this cannot be true!

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
char m
  • 7,840
  • 14
  • 68
  • 117

5 Answers5

4

Although the question is not quite clear it sounds like you have a group of rows from one table and want to update the equivalent rows in another datatable. If this is the case they you can just use the find method and manually update them as you suggested, or alternatively, add the new rows to another table and merge them (there are all sorts of options for merging two data tables). Merging however will just do the same thing under the hood (i.e. find by primary key and update the columns).

Another way would be to just replace the row and set its status to modified datarow.SetModified()

Mike Hanrahan
  • 1,192
  • 8
  • 18
  • thanks! i admit question not clear at all. that's partly because what I wanted to do was maybe stupid. this answered my question though. – char m Jan 09 '12 at 17:18
1

Or you can delete the old one and add the new one...

  • that wouldn't solve anything for 2 reasons: for first: I still have to make criteria to find it. for second it would have wrong row status when it's written back to database. – char m Jan 09 '12 at 15:23
  • If your new rows don't exist in the datatable then you will always have to provide some criteria to find them...there is no magic (a merge will find them based on primary key). Also, you can set the row status use 'row.SetModified()' which will solve the issue once you go to save the row. – Mike Hanrahan Jan 09 '12 at 16:12
  • @mike: thaks. ok for status part but what you mean by "a merge will find them based on primary key"? I understand now from your answer. so no need to answer. – char m Jan 09 '12 at 17:14
0

Another option is to use the ItemArray property to update the DataRow instance. The only thing is that you still have to select the proper rows. Also, note that you have to change the whole ItemArray instead of its elements in order to have the modification reflected in the DataTable.

foreach (DataRow row in rows)
{
   // Select the row
   var rows = _dataSet.Tables[tableName].Select(string.Format("Table_ID = {0}", row["Table_ID"]));

   // Update the row
   if (0 < rows.Length)
       rows[0].ItemArray = (object[])row.ItemArray.Clone();
}

Regardless of the way you chose to update the DataRow, you can put the whole thing in an extension method.

Community
  • 1
  • 1
Teorist
  • 86
  • 8
0

Could you use boxing to do that if your datatables are the same.

 DestDataset.DestDataTable newChildRecords = 
 (DestDataset.DestDataTable)_dataset.Tables[tableName].GetChanges(DataRowState.Added);
Turbot
  • 5,095
  • 1
  • 22
  • 30
0

If you are using dataAdapter...

foreach (DataRow row in rows)
{
    Row.BeginEdit();        
    //Do your stuff        
    Row.EndEdit();
}
dataAdapter.Update(_dataSet, "yourTable");
m.rufca
  • 2,558
  • 2
  • 19
  • 26