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

Re: Deleting a bound DataGridView row? by Bart

Bart
Tue Jan 03 06:30:57 CST 2006

Hi,

"Michael Wong" <nospam@email.here> wrote in message
news:%23M$88wEEGHA.532@TK2MSFTNGP15.phx.gbl...
>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



Re: Deleting a bound DataGridView row? by Cor

Cor
Tue Jan 03 06:50:07 CST 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



Re: Deleting a bound DataGridView row? by Joanna

Joanna
Tue Jan 03 10:18:40 CST 2006

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

| 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



Re: Deleting a bound DataGridView row? by Cor

Cor
Tue Jan 03 10:30:49 CST 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



Re: Deleting a bound DataGridView row? by Michael

Michael
Tue Jan 03 13:11:21 CST 2006

Hi Bart,

It works!
Now I can continue on.

Thank you very much,

Michael