HighTechTalks DotNet Forums  

Deleting a bound DataGridView row?

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss Deleting a bound DataGridView row? in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Michael Wong
 
Posts: n/a

Default Deleting a bound DataGridView row? - 01-03-2006 , 04:57 AM






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.Ro ws.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
  #2  
Old   
Bart Mermuys
 
Posts: n/a

Default Re: Deleting a bound DataGridView row? - 01-03-2006 , 07:30 AM






Hi,

"Michael Wong" <nospam (AT) email (DOT) here> wrote

Quote:
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_masterDataG ridView.SelectedRows[0]);

Quote:
Debug.WriteLine(myTypedDataset.myTypedDataTable.Ro ws.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
  #3  
Old   
Cor Ligthert [MVP]
 
Posts: n/a

Default Re: Deleting a bound DataGridView row? - 01-03-2006 , 07:50 AM



Quote:
Use something like:
while (tbl_masterDataGridView.SelectedRows.Count > 0)

tbl_masterDataGridView.Rows.Remove(tbl_masterDataG ridView.SelectedRows[0]);


Or use the For index backwards

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

Cor




Reply With Quote
  #4  
Old   
Joanna Carter [TeamB]
 
Posts: n/a

Default Re: Deleting a bound DataGridView row? - 01-03-2006 , 11:18 AM



"Cor Ligthert [MVP]" <notmyfirstname (AT) planet (DOT) nl> a écrit dans le message de
news: OKy%23lQGEGHA.336 (AT) TK2MSFTNGP14 (DOT) phx.gbl...

Quote:
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
  #5  
Old   
Cor Ligthert [MVP]
 
Posts: n/a

Default Re: Deleting a bound DataGridView row? - 01-03-2006 , 11:30 AM



Quote:
| 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
  #6  
Old   
Michael Wong
 
Posts: n/a

Default Re: Deleting a bound DataGridView row? - 01-03-2006 , 02:11 PM



Hi Bart,

It works!
Now I can continue on.

Thank you very much,

Michael

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.