Home All Groups Group Topic Archive Search About

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

Author
25 Apr 2005 5:07 PM
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.

Author
25 Apr 2005 6:00 PM
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen



Show quoteHide quote
> 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.
>
Author
26 Apr 2005 9:48 AM
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.




Show quoteHide quote
"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:544378632500344166316288@msnews.microsoft.com...
> 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.
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
> > 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.
> >
>
>
>