DataRowView - deleting rows: A little quiz -or- Why doesn't this work...

C

Chris Mayers

Can anyone please tell me what is wrong with the following code:

I have a datagrid showing data from in a DataView.

The following code snippet is supposed to delete any rows where the
'LineType' column contains WD.

The strange thing is it works sometimes (10%?) but usually I get an error:
'Specified argument was out of the range of valid values. Parameter name:
rowIndex'

Please no comments on my error handling, this is just to show the errors I'm
getting...

DataView dv = (DataView)dataGrid1.DataSource;
dv.Sort = "LineType";
dv.AllowDelete = true;
DataRowView[] rowsToDelete = dv.FindRows("WD");
foreach(DataRowView rowToDelete in rowsToDelete)
{
try
{
rowToDelete.Delete();
//also tried : rowsToDelete[0].Delete();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

If anyone can tell me what I'm doing wrong, even if it is simply that I'm
trying to do somthing you're not supposed to do, I would be most grateful.

Thanks,

Chris.
 
B

Brock Allen

Well, rowIndex out of range sounds like the collection is getting reordered
when you delete a row. So the DataRowView internally just has a row index
into the original collection. So when you get to a row further down in the
collecion, then original row number the DataRowView held is no longer valid.
IOW, I go to row 5, say, and delete it, then I go to what used to be row
6, and now it's row 5 since they all got bumped up in the collection.

I'd suggest getting the DataRowView.Row and then deleting that. So it might
mean that you'll need to do a first pass over the collection to get an array
of the real DataRows then delete those.
 
C

Chris Mayers

Hi Brock,

Thanks for your suggestion. Sorry to have bothered you though!

Please try not to laugh...

The reason that it wasn't working is that I had some code in the
'RowDeleting' event of the datatable that all this was based on that I had
forgotton about (Doh!) The error was getting thrown by THAT code when the
code I published here was trying to delete the row...
Thanks for your help though, it was only whilst writing a reply to your
previous post in this thread that the horrible truth suddenly dawned on me!

PS. I did use your suggestion anyway as the DataRowView[] collection does
seem to behave slightly strangely when you start deleting things from it,
and I was happier dealing with the actual DataRows.

Cheers,

Chris.




Brock Allen said:
Well, rowIndex out of range sounds like the collection is getting reordered
when you delete a row. So the DataRowView internally just has a row index
into the original collection. So when you get to a row further down in the
collecion, then original row number the DataRowView held is no longer valid.
IOW, I go to row 5, say, and delete it, then I go to what used to be row
6, and now it's row 5 since they all got bumped up in the collection.

I'd suggest getting the DataRowView.Row and then deleting that. So it might
mean that you'll need to do a first pass over the collection to get an array
of the real DataRows then delete those.




Can anyone please tell me what is wrong with the following code:

I have a datagrid showing data from in a DataView.

The following code snippet is supposed to delete any rows where the
'LineType' column contains WD.

The strange thing is it works sometimes (10%?) but usually I get an
error: 'Specified argument was out of the range of valid values.
Parameter name: rowIndex'

Please no comments on my error handling, this is just to show the
errors I'm getting...

DataView dv = (DataView)dataGrid1.DataSource;
dv.Sort = "LineType";
dv.AllowDelete = true;
DataRowView[] rowsToDelete = dv.FindRows("WD");
foreach(DataRowView rowToDelete in rowsToDelete)
{
try
{
rowToDelete.Delete();
//also tried : rowsToDelete[0].Delete();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
If anyone can tell me what I'm doing wrong, even if it is simply that
I'm trying to do somthing you're not supposed to do, I would be most
grateful.

Thanks,

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,079
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top