PC Review


Reply
Thread Tools Rate Thread

Deleting a bound DataGridView row?

 
 
Michael Wong
Guest
Posts: n/a
 
      3rd Jan 2006
I have a DataGridView which is bound to a dataset.

When I delete rows from the DataGridView, should it delete the same rows
from the dataset also?
I have tried the following code, but apparently, the dataset isn't
updated. Did I miss (understood) something?

for(int i = 0; i < dataGridView.SelectedRows.Count; i++)
{
DataGridViewRow row = dataGridView.SelectedRows[i];
dataGridView.Rows.Remove(row);
}

Debug.WriteLine(myTypedDataset.myTypedDataTable.Rows.Count.ToString());
// Here, the count is the same as before the deletion, but when
// accessing the dataTable rows, a
// DeletedRowInaccessibleException occurs on the deleted rows
 
Reply With Quote
 
 
 
 
Bart Mermuys
Guest
Posts: n/a
 
      3rd Jan 2006
Hi,

"Michael Wong" <(E-Mail Removed)> wrote in message
news:%23M$(E-Mail Removed)...
>I have a DataGridView which is bound to a dataset.
>
> When I delete rows from the DataGridView, should it delete the same rows
> from the dataset also?
> I have tried the following code, but apparently, the dataset isn't
> updated. Did I miss (understood) something?
>
> for(int i = 0; i < dataGridView.SelectedRows.Count; i++)
> {
> DataGridViewRow row = dataGridView.SelectedRows[i];
> dataGridView.Rows.Remove(row);
> }


This will delete the DataRows, but once you remove a DataGridViewRow from
dataGridView.Rows, then it will also be removed from SelectedRows, so the
SelectedRows.Count will decrease and not all SelectedRows will be deleted.

Use something like:
while (tbl_masterDataGridView.SelectedRows.Count > 0)
tbl_masterDataGridView.Rows.Remove(tbl_masterDataGridView.SelectedRows[0]);

>
> Debug.WriteLine(myTypedDataset.myTypedDataTable.Rows.Count.ToString());
> // Here, the count is the same as before the deletion, but when
> // accessing the dataTable rows, a
> // DeletedRowInaccessibleException occurs on the deleted rows


That's both normal, deleted rows are still visible inside the DataTable.Rows
collection, but if you want to access their data then you must use an
overloaded version of DataRow[] which takes a DataRowVersion. Because
deleted rows only have an Original row version, example:

foreach (DataRow dr in myTypedDataset.myTypedDataTable.Rows)
{
Console.Write(dr.RowState + " ");
foreach (DataColumn dc in myTypedDataset.myTypedDataTable.Columns)
{
if (dr.RowState == DataRowState.Deleted)
Console.Write(dr[dc, DataRowVersion.Original]+" ");
else
Console.Write(dr[dc]+ " ");
}
Console.WriteLine();
}

The deleted rows will be removed from the DataTable.Rows collection once you
perform a DataAdapter.Update or a DataTable.AcceptChanges (but don't do this
if you're planning to do a DataAdapter.Update).


HTH,
Greetings


 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      3rd Jan 2006
>
> Use something like:
> while (tbl_masterDataGridView.SelectedRows.Count > 0)
>
> tbl_masterDataGridView.Rows.Remove(tbl_masterDataGridView.SelectedRows[0]);
>
>>

Or use the For index backwards

for(int i = dataGridView.SelectedRows.Count; i < 0; i--)

Cor


 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      3rd Jan 2006
"Cor Ligthert [MVP]" <(E-Mail Removed)> a écrit dans le message de
news: OKy%(E-Mail Removed)...

| for(int i = dataGridView.SelectedRows.Count; i < 0; i--)

or possibly

for(int i = dataGridView.SelectedRows.Count; i > 0; i--)

might work better :-))

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      3rd Jan 2006
> | for(int i = dataGridView.SelectedRows.Count; i < 0; i--)
>
> or possibly
>
> for(int i = dataGridView.SelectedRows.Count; i > 0; i--)
>
> might work better :-))
>

Without any doubt

:-)

Cor


 
Reply With Quote
 
Michael Wong
Guest
Posts: n/a
 
      3rd Jan 2006
Hi Bart,

It works!
Now I can continue on.

Thank you very much,

Michael
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New Row in the bound DataGridView D2 Microsoft C# .NET 0 10th Apr 2007 03:46 PM
Deleting rows in a DatagridView bound to an XmlDataDocument Troy McLure Microsoft Dot NET Framework Forms 0 29th Aug 2006 09:41 PM
Adding rows to a bound table that is bound to DataGridView(DataSou =?Utf-8?B?S2V2aW4gQnVydG9u?= Microsoft Dot NET Framework Forms 2 22nd Jun 2006 06:05 AM
Get object which DataGridView row is bound to moondaddy Microsoft Dot NET Framework Forms 0 11th Apr 2006 05:18 AM
Refreshing datagridview contents after a failed delete in bound datagridview Bob Microsoft VB .NET 0 5th Jan 2006 06:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:58 PM.