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

Discussion in 'ASP .Net Datagrid Control' started by Chris Mayers, Apr 25, 2005.

  1. Chris Mayers

    Chris Mayers Guest

    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.
     
    Chris Mayers, Apr 25, 2005
    #1
    1. Advertising

  2. Chris Mayers

    Brock Allen Guest

    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.
    >
     
    Brock Allen, Apr 25, 2005
    #2
    1. Advertising

  3. Chris Mayers

    Chris Mayers Guest

    Re: DataRowView - deleting rows: A little quiz -or- Why doesn't this work...BECAUSE I'M AN IDIOT!!

    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" <> wrote in message
    news:...
    > 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.
    > >

    >
    >
    >
     
    Chris Mayers, Apr 26, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. karim
    Replies:
    5
    Views:
    3,038
    David Waz...
    Jul 11, 2003
  2. Replies:
    3
    Views:
    2,476
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,222
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    6
    Views:
    328
    Old Wolf
    Mar 14, 2005
  5. Karen
    Replies:
    2
    Views:
    107
    Vlad Tepes
    Aug 31, 2003
Loading...

Share This Page