Search code examples
c#ado.nettableadapter

Quick way to insert the same record as copy using Tableadapter


I have a datatable with a bunch of fields, and it's filled data by tableadapter. Usually, records that need to be inserted are a little bit different (only id). I don't want user to retypes everything again so, is there a quick way to insert current record into database. Notice that all my insert, update and delete operations are based on my tableadapter.

//Get only one last record that has been added when the form load.
bookTableAdapter.FillByLastID(bookDataSet.dtBook);

private void btnNew_Click(object sender, EventArgs e)
{  
  bookBindingSource.AddNew();  
}

private void btnSave_Click(object sender, EventArgs e)
{
  this.Validate();
  bookBindingSource.EndEdit();
  bookTableAdapter.Update(bookDataSet.dtBook);
}

Solution

  • Try this to copy row:

            DataRow row = table.Rows[0]; // row you want to copy
    
            DataRow newRow = table.NewRow();
            newRow.ItemArray = row.ItemArray.ToArray();
            newRow["Id"] = 10;  // change Id
    
            table.Rows.Add(newRow);