locked
removing rows too slow in datagridview RRS feed

  • Question

  • Hi,

    I have this code:

    If kivertekhossz = 2 And dgvszamlatukor.Rows(ind).Cells(1).Value.ToString.Length > 2 Then<br/>
            dgvszamlatukor.SelectedRows(0).Cells(0).Value = My.Resources.S_CLOFOL<br/>
            Do<br/>
              dgvszamlatukor.Rows.RemoveAt(ind)<br/>
              If ind = dgvszamlatukor.RowCount Then Exit Sub<br/>
            Loop While dgvszamlatukor.Rows(ind).Cells(1).Value.ToString.Length > 2<br/>
          End If
    

    The focus is on the RemoveAt method. It's too slow. And there are just 120 rows to remove. It takes appr. 3 sec to remove them. But when I populate the dgv, it happens immediately.

    I searched the net, but SuspendLayout and ResumeLayout and DoubleBuffering didn't work.

    Any suggestion?

    Monday, July 26, 2010 5:02 PM

Answers

  • I solved it already. I searched more and more and I found a solution: I must set the autosizemode (rows and columns) to none. But I need that the cells fit to the content, I can make this with the autoresizecolumn event. So everything is fine now. I must just solve the issue, that the padding of the icons slows very the ADDING of the rows, but the removing not.
    • Marked as answer by DerStauner Tuesday, July 27, 2010 5:41 PM
    Tuesday, July 27, 2010 5:41 PM

All replies

  • Can you confirm that you have no extra code being performed in an event that might be raised during the removal of a row (i.e. RowsRemoved event)?  I would think that the SuspendLayout call would have some effect because the rendering of the changes in the DGV would no longer be competing with the resources surrounding the removals.  Is it possible that a call to ResumeLayout is implicitly happening somewhere in your other event handlers?  This may be an internal call within the DGV -- I seem to recall my encountering an issue like that once but the details are gone now.
    Monday, July 26, 2010 5:09 PM
  • I have only a cellmouseclick and a cellmousedoubleclick event. So, I think, I can confirm it, that I have no such extra code.

    I have founded this, but I can't figure out how to apply this in my case:

    http://www.eggheadcafe.com/software/aspnet/30443261/why-is-removing-rows-in-d.aspx

    It is about the following: the user clicks on a cell, the app examines the content of this cell, and depending on this, rows are added or removed. Some kind of a treeview. http://yfrog.com/3dimageunwj

    Monday, July 26, 2010 5:54 PM
  • If you do not remove a row, how does the index change? You might get a better result by merely saving the indices you wish to remove and rebuilding the DGV by only inserting those that you want to preserve. When you do it the way that you have, the collection must be rebuilt each time you remove an item, and the index then points at the next item in the collection, but the value of the index itself is not changing. That's a very error prone way to do this, and it will never be very efficient. It is the same as finding out something by eliminating everything that it isn't and redoing the set each time you ask a question.
    • Edited by jinzai Monday, July 26, 2010 7:08 PM typo
    Monday, July 26, 2010 7:08 PM
  • "If you do not remove a row, how does the index change?"

    I select an opened subgroup and I remove always the row below (selectedrows(0).index+1) The index don't change, I don't need this, because when I remove a row, the selectedrows(0).index+1 points to the next row to be removed.

    "You might get a better result by merely saving the indices you wish to remove and rebuilding the DGV by only inserting those that you want to preserve."

    It's very difficult to do this, because I must store before the dgv.rows.clear, which group or subgroup was open or closed. That isn't a good idea, because at every click on a group would load the entire dgv. It contains at this time appr. 800 rows.

    "the collection must be rebuilt each time you remove an item"

    Please explain this, I don't understand, why the collection must be rebuilt. I just add or remove some rows.

    "the index then points at the next item in the collection, but the value of the index itself is not changing"

    Is this a problem?

     

    Tuesday, July 27, 2010 4:33 PM
  • Could you delete all the rows and repopulate excluding the rows you don't want?

    How about this:

                    .SuspendLayout()
                    .RowCount = 0
                    .ColumnCount = 0
                    .ResumeLayout()
    Tuesday, July 27, 2010 5:23 PM
  • I solved it already. I searched more and more and I found a solution: I must set the autosizemode (rows and columns) to none. But I need that the cells fit to the content, I can make this with the autoresizecolumn event. So everything is fine now. I must just solve the issue, that the padding of the icons slows very the ADDING of the rows, but the removing not.
    • Marked as answer by DerStauner Tuesday, July 27, 2010 5:41 PM
    Tuesday, July 27, 2010 5:41 PM