Home All Groups Group Topic Archive Search About

Adding rows to table from DataGridView

Author
25 Dec 2006 2:52 PM
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.

Author
25 Dec 2006 5:19 PM
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.
-------------------------------------
Show quote
"Adel Khalil" <adel***@hotmail.com> wrote in message
news:534D21FE-22EF-44A0-881F-76B27CF06DE2@microsoft.com...
> 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.
Author
25 Dec 2006 5:48 PM
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 ?
Author
26 Dec 2006 3:14 AM
RobinS
Show quote
"Adel Khalil" <adel***@hotmail.com> wrote in message
news:F6562F42-A630-4507-A9AE-D842EEA83F6B@microsoft.com...
> 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.
Author
26 Dec 2006 7:37 AM
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

Show quote
"RobinS" <RobinS@NoSpam.yah.none> schreef in bericht
news:9PadnQt3-cAtDg3YnZ2dnUVZ_qmpnZ2d@comcast.com...
>
> "Adel Khalil" <adel***@hotmail.com> wrote in message
> news:F6562F42-A630-4507-A9AE-D842EEA83F6B@microsoft.com...
>> 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.
>
Author
26 Dec 2006 7:50 AM
Cor Ligthert [MVP]
doh,

forgot that sentence "no change or"

Cor

Show quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
news:%23TlbUBMKHHA.1064@TK2MSFTNGP04.phx.gbl...
> 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...
>>
>> "Adel Khalil" <adel***@hotmail.com> wrote in message
>> news:F6562F42-A630-4507-A9AE-D842EEA83F6B@microsoft.com...
>>> 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.
>>
>
>
Author
26 Dec 2006 5:11 PM
RobinS
Thanks for clarifying the difference between Remove and Delete for me.
I appreciate it.
Robin S.
-------------------------------
Show quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:O%23bKCJMKHHA.5000@TK2MSFTNGP03.phx.gbl...
> doh,
>
> forgot that sentence "no change or"
>
> Cor
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
> news:%23TlbUBMKHHA.1064@TK2MSFTNGP04.phx.gbl...
>> 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...
>>>
>>> "Adel Khalil" <adel***@hotmail.com> wrote in message
>>> news:F6562F42-A630-4507-A9AE-D842EEA83F6B@microsoft.com...
>>>> 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.
>>>
>>
>>
>
>
Author
26 Dec 2006 5:49 AM
Manish Bafna
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.


Show quote
"Adel Khalil" wrote:

> 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 ?
>
Author
26 Dec 2006 7:34 AM
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


Show quote
"Adel Khalil" <adel***@hotmail.com> schreef in bericht
news:534D21FE-22EF-44A0-881F-76B27CF06DE2@microsoft.com...
> 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.

AddThis Social Bookmark Button