Re-added row

  • Thread starter Thread starter nvx
  • Start date Start date
N

nvx

Hello there,
I need an advice on "re-adding the deleted rows" in a DataTable. The
situation is as follows:

1. I have a data-bound DataGridView.
2. User wants to delete a row with a key value "KEY", therefore this
row gets deleted using
DataRow row = dataSet.Tables["data"].Rows.Find("KEY");
row.Delete();
3. The deleted row is still in the DataTable, but with
DataRowState.Deleted.
4. Now, user decides to add a row with the same key value again, so he
types it into the DataGridView. At this moment, there are TWO rows
with the same key value "KEY": one with DataRowState.Deleted and one
with DataRowState.Added.
5. User wants to save the data into the .mdb file, therefore
tableAdapter.Update(...) is called ending up with a concurrency error
saying that the UpdateCommand event caused 0 out of 1 records to
update (or something similar, I unfortunately don't have an English
version of .NET).

What should I do with these two DataRows to be able to use the
Update(...) command later on?

Any help will be much appreciated.


Best regards,
nvx
 
Dear Cor,
thank you very much for your advice. Nevertheless, I can't find a way
to get this working. Could you possibly give some more info or a few
links?

Currently, I do the adding in the dataGridView_RowValidated(...)
method which looks like the following:

bool rowRecovery = false;
DataRow deletedRow;
DataRow newDataRow;
....
dataGridView_RowValidated(object sender, DataGridViewCellEventArgs e)
{
if (rowRecovery)
{
deletedRow.RejectChanges();
for (int i = 0; i < newDataRow.ItemArray.Length; i++)
{
deletedRow = newDataRow;
}
rowRecovery = false;
}
else
{
string id =
dataGridView.Rows[e.RowIndex].Cells[idxID].EditedFormattedValue.ToString();
bool deletedExists = false;
DataRow addedRow = dataSet.Tables["table"].NewRow();
deletedRow = dataSet.Tables["table"].NewRow();
newDataRow = dataSet.Tables["table"].NewRow();
DataView view = new DataView(dataSet.Tables["table"], null,
null, DataViewRowState.Deleted);
foreach (DataRowView rv in view)
{
if ((string)rv.Row["id", DataRowVersion.Original] ==
id)
{
addedRow = dataSet.Tables["table"].Rows.Find(id);
deletedRow = rv.Row;
deletedExists = true;
break;
}
}
if (deletedExists)
{
for (int i = 0; i < addedRow.ItemArray.Length; i++)
{
newDataRow = addedRow;
}
rowRecovery = true;
addedRow.RejectChanges(); // this causes a new row
validation, hence we move to the first line in this method
}
}
}


Still, it throws the Concurrency error after using the .Update
command. Where might the problem be?

I'd be very grateful for any additional info on this.


Best regards,
nvx
 
Dear Cor,
thank you for your reply. It seems it works okay now. :) I'll just do
some tests to be sure.

Thanks a lot, again.

Best regards,
nvx
 
Back
Top