Adding rows to table from DataGridView


A

Adel Khalil

hello, am in a bit of a problem.. wonder if anyone can help?

I got two tables one is tblOrders and the other is tblDeletedOrders
I'm handling the UserDeletingRow to insert the deleted row (order) into the
tblDeletedOrders to sort out some kind archive or log.

I'm using this code which is evaluating to nothing and after delete I find
the tblDeletedOrders empty.

private void dgvOrders_UserDeletingRow(object sender,
DataGridViewRowCancelEventArgs e)
{ DataRow dr = ((DataRowView)(e.Row.DataBoundItem)).Row;
dataFilesDataSet.tblDeletedOrders.ImportRow(dr);
}

thanks in advance.
 
Ad

Advertisements

R

RobinS

What if you use
dataFilesDataSet.tblDeletedOrders.Rows.Add(dr)
instead?

And what do you mean the code is evaluating to nothing?
And are you invoking EndEdit to save the changes to the dataset?

Robin S.
 
A

Adel Khalil

what I meant by evaluating to nothing is the code runs but nothing happened,
I used import row instead of Add method as the add method return err This
row already belongs to another table. even if I remove the row from the row
collection also used EndEdit().

DataRow dr = ((DataRowView)(e.Row.DataBoundItem)).Row;
dataFilesDataSet.tblOrders.Rows.Remove(dr);
dataFilesDataSet.tblDeletedOrders.Rows.Add(dr);
dataFilesDataSet.EndInit();

thanks for your reply.
what should I do now ?
 
R

RobinS

Adel Khalil said:
what I meant by evaluating to nothing is the code runs but nothing
happened, I used import row instead of Add method as the add method
return err This row already belongs to another table. even if I remove
the row from the row collection also used EndEdit().

DataRow dr = ((DataRowView)(e.Row.DataBoundItem)).Row;
dataFilesDataSet.tblOrders.Rows.Remove(dr);
dataFilesDataSet.tblDeletedOrders.Rows.Add(dr);
dataFilesDataSet.EndInit();

thanks for your reply.
what should I do now ?
This code was posted here a couple of days ago by a MSFT in response to
another
post titled "Move DataRow to other DataTable".


//copy rowdata from table1 to table2
dataTable2.Rows.Add(row.ItemArray);
//remove rowdata from table1
dataTable1.Rows.Remove(row);

Does that work?

Robin S.
 
G

Guest

Hi,
Try this code:

DataRow dr = ((DataRowView)(e.Row.DataBoundItem)).Row;
dataFilesDataSet.tblOrders.Rows.Remove(dr);

Dataset dataFilesDataSet2 = dataFilesDataSet.Copy();
dataFilesDataSet2.tblDeletedOrders.Rows.Add(dr);

Thanks and Regards,
Manish Bafna.
MCP and MCTS.
 
C

Cor Ligthert [MVP]

Adel,

I am not sure what you want to achieve, however in my idea should your code
work if you want to presieve your deleted rows.

The datarowstate in the copy stays deleted. It is a copy not a shalow copy
in this case.
The behaviour you describes is as with a shallowcopy and that is different
to show it.


This code is working
\\\
DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("DC1"));
dt1.Rows.Add(dt1.NewRow());
dt1.Rows[0][0] = "Adel";
DataTable dt2 = dt1.Clone();
dt2.ImportRow(dt1.Rows[0]);
dt1.Rows[0].Delete();
///

Be aware that you cannot import an detached (deleted or removed) row.

Cor
 
Ad

Advertisements

C

Cor Ligthert [MVP]

Robin,

Be aware that the behaviour from Remove and Delete is very different.

"Remove" removes a row from the datatable.
"Delete" sets the DataRowState to "deleted", this of course not at rows
which has no change or are added in the session. Those are removed with a
Delete as well.

You can say that Remove is a Delete with an inbuild acceptchanges for that
row.

Cor
 
C

Cor Ligthert [MVP]

doh,

forgot that sentence "no change or"

Cor

Cor Ligthert said:
Robin,

Be aware that the behaviour from Remove and Delete is very different.

"Remove" removes a row from the datatable.
"Delete" sets the DataRowState to "deleted", this of course not at rows
which has no change or are added in the session. Those are removed with a
Delete as well.

You can say that Remove is a Delete with an inbuild acceptchanges for that
row.

Cor
 
Ad

Advertisements

R

RobinS

Thanks for clarifying the difference between Remove and Delete for me.
I appreciate it.
Robin S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top