Groups | Blog | Home
all groups > dotnet ado.net > december 2006 >

dotnet ado.net : Adding rows to table from DataGridView


RobinS
12/25/2006 9:19:37 AM
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.
-------------------------------------
[quoted text, click to view]

Adel Khalil
12/25/2006 4:52:30 PM
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.
RobinS
12/25/2006 7:14:52 PM

[quoted text, click to view]

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.

Adel Khalil
12/25/2006 7:48:42 PM
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 ?
Manish Bafna
12/25/2006 9:49:00 PM
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.


[quoted text, click to view]
Cor Ligthert [MVP]
12/26/2006 12:00:00 AM
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


"Adel Khalil" <adel83k@hotmail.com> schreef in bericht
news:534D21FE-22EF-44A0-881F-76B27CF06DE2@microsoft.com...
[quoted text, click to view]

Cor Ligthert [MVP]
12/26/2006 12:00:00 AM
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

"RobinS" <RobinS@NoSpam.yah.none> schreef in bericht
news:9PadnQt3-cAtDg3YnZ2dnUVZ_qmpnZ2d@comcast.com...
[quoted text, click to view]

Cor Ligthert [MVP]
12/26/2006 12:00:00 AM
doh,

forgot that sentence "no change or"

Cor

"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> schreef in bericht
news:%23TlbUBMKHHA.1064@TK2MSFTNGP04.phx.gbl...
[quoted text, click to view]

RobinS
12/26/2006 9:11:24 AM
Thanks for clarifying the difference between Remove and Delete for me.
I appreciate it.
Robin S.
-------------------------------
[quoted text, click to view]

AddThis Social Bookmark Button