PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Adding rows to table from DataGridView

Reply

Adding rows to table from DataGridView

 
Thread Tools Rate Thread
Old 25-12-2006, 02:52 PM   #1
Adel Khalil
Guest
 
Posts: n/a
Default Adding rows to table from DataGridView


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.

  Reply With Quote
Old 25-12-2006, 05:19 PM   #2
RobinS
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView

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.
-------------------------------------
"Adel Khalil" <adel83k@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.



  Reply With Quote
Old 25-12-2006, 05:48 PM   #3
Adel Khalil
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView

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 ?

  Reply With Quote
Old 26-12-2006, 03:14 AM   #4
RobinS
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView


"Adel Khalil" <adel83k@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.


  Reply With Quote
Old 26-12-2006, 05:49 AM   #5
=?Utf-8?B?TWFuaXNoIEJhZm5h?=
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView

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.


"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 ?
>

  Reply With Quote
Old 26-12-2006, 07:34 AM   #6
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView

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...
> 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.



  Reply With Quote
Old 26-12-2006, 07:37 AM   #7
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView

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" <adel83k@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.
>



  Reply With Quote
Old 26-12-2006, 07:50 AM   #8
Cor Ligthert [MVP]
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView

doh,

forgot that sentence "no change or"

Cor

"Cor Ligthert [MVP]" <notmyfirstname@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" <adel83k@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.
>>

>
>



  Reply With Quote
Old 26-12-2006, 05:11 PM   #9
RobinS
Guest
 
Posts: n/a
Default Re: Adding rows to table from DataGridView

Thanks for clarifying the difference between Remove and Delete for me.
I appreciate it.
Robin S.
-------------------------------
"Cor Ligthert [MVP]" <notmyfirstname@planet.nl> wrote in message
news:O%23bKCJMKHHA.5000@TK2MSFTNGP03.phx.gbl...
> doh,
>
> forgot that sentence "no change or"
>
> Cor
>
> "Cor Ligthert [MVP]" <notmyfirstname@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" <adel83k@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.
>>>

>>
>>

>
>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off