none
deleting multi rows in datagridview backwards RRS feed

  • Question

  • HI all,

    I have a strange problem. 

    I am deleting a group of rows from datagridview backwards with a code like this

    For I = DGV.Rows.Count - 1 To 0 Step -1
    
    If dgv.rows(I).selected = True
    ' delete row from DGV
    End If
    
    next

    Now the problem is that if I delete a particular row in DGV another object delete automatically the row preceding that one.

    For example , the DGV has 50 rows and I want to delete rows form 25 to 15 (backwoards)

    When I = 10 another object delete row 9 automatically 

    Now I = 9 and the loop try to read row 9 that does not exist anymore,  so I get error "out of index"

    The question is : How can I check if  row(9) does not exist any more ?

    p.s.

    This problem comes using DatagridviewGrouper 

    The DatagridviewGrouper create and delete automatically grouping rows and delete a grouping row if user delete all rows in the group ( but this is a details) 

    Thank you for help

    Monday, August 6, 2018 4:41 PM

Answers

  • That's acceptable if you don't have a way to detect the grouping rows and skip them.  There's some overhead associated with catching the exception, but for the few iterations the loop will go through it will probably not be noticeable.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Sunday, August 26, 2018 5:42 PM
    Wednesday, August 8, 2018 12:44 AM
    Moderator

All replies

  • It might be easer to just get the list of selected rows, reverse it, and then remove each row:

    Dim rows = dgv.SelectedRows.Cast(Of DataGridViewRow).Reverse
    For Each row In rows
        dgv.Rows.Remove(row)
    Next
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, August 6, 2018 6:19 PM
    Moderator
  • Hi Reed

    this does not solve the problem .

    The DGV is bound to data table and I have to delete rows in datatable. ( And sql database)

    DatagridviewGrouper add to dgv new rows (the Groupinf rows)

    After deleting all rows in a Group (Using DatagridviewGrouper) this one deletes automatically the GroupRow and nothing deletes that row in your array

    So traing to delete ROWS looping your array gives the same problem (Out of index)

    I add the image of DGV using DatagridviewGrouper


    In Yellow the rows to be deleted  (selected with a checkbox column) , in Gray the GrouRows automatically added by DatagridviewGrouper when Groupping is activadetd

    These are the rows automatically deleted from DGV when all rows of a Group are deleted. 

    In the image the group row "AQ" will be deleted automatically but it is still in your array when looping it.

    Tuesday, August 7, 2018 8:10 AM
  • Where does DatagridviewGrouper come from?

    Does it have its own documentation? We can't support 3rd party products here and this sounds like it is either a bug in that software or that it has guidance which isn't being followed.

    Maybe those 'group' rows have their own type or a property that you can identify and then skip them in the loop when calling Remove.  That's all I can think of.  Again this sounds like buggy behavior in that grouping code.  It doesn't make sense that the grouping row should be part of the selected rows collection.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, August 7, 2018 11:13 AM
    Moderator
  • Hi Reed

    the DatagridviewGrouper comes from Codeproject

    This is the link

    https://www.codeproject.com/Tips/995958/DataGridViewGrouper

    In any case forget DataGridViewGrouper and let us go back to my first question:

    For example , the DGV has 50 rows and I want to delete rows form 25 to 15 (backwoards)

    I loop thru DGV rows with

    For I = DGV.Rows.Count - 1 To 0 Step -1

    'if row checked

    ...delete row

    'end if

    Next

    .When i = 10 another object deletes row 9 automatically 

    Now i =  9 and the loop try to read row 9 that does not exist anymore,  so I get error "out of index"

    The question is : How can I check if  row(9) does not exist any more ?

    The only solution I found for now  is to use Try...Catch before Loop, and if row= 9 does not exist any more I get an exception that I do not handle ,  so i goes  to 8 without "out of range" error.

    For I = DGV.Rows.Count - 1 To 0 Step -1

    Try

    '---If row checked

    '...delete row

    '----end if

    catch

    end try

    next



    Do you think it is  a good solution  to you ?

    Tuesday, August 7, 2018 9:43 PM
  • That's acceptable if you don't have a way to detect the grouping rows and skip them.  There's some overhead associated with catching the exception, but for the few iterations the loop will go through it will probably not be noticeable.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Claudio111 Sunday, August 26, 2018 5:42 PM
    Wednesday, August 8, 2018 12:44 AM
    Moderator