Removing/Appending Rows in a Grid

Unfortunately this is yet another grid resize question.
I’ve attached a foo.py that demonstrates my problem.
When the refresh button is pressed, the data (just a simple python list) gets modified (grows or shrinks).

I’m trying to get the grid to re-create itself (or update itself) when this happens.

I’m following the example here: http://wiki.wxpython.org/wxGrid
But for some reason the number of rows just keeps growing.

I suspect the bug lies in this part of the code:
/// myDataList has just been modified (randomly added or removed rows), so now it’s time to update the grid to display this new data:

    self.grid.BeginBatch()
    current, new, delmsg, addmsg = (len(self.myDataList), self.grid.GetNumberRows(), wxGRIDTABLE_NOTIFY_ROWS_DELETED, wxGRIDTABLE_NOTIFY_ROWS_APPENDED)
    if new < current:
            print 'deleting rows:', current-new
            msg = wx.grid.GridTableMessage(
                    self.grid.GetTable(),
                    delmsg,
                    new,    # position
                    current-new,
            )
            self.grid.ProcessTableMessage(msg)
    elif new > current:
            print 'adding rows: ', new-current
            msg = wx.grid.GridTableMessage(
                    self.grid.GetTable(),
                    addmsg,
                    new-current
            )
            self.grid.ProcessTableMessage(msg)
   
    msg = wx.grid.GridTableMessage(self.grid.GetTable(), wxGRIDTABLE_REQUEST_VIEW_GET_VALUES)
    self.grid.ProcessTableMessage(msg)
    self.grid.EndBatch()

Any ideas what could be wrong? It’s driving me nuts!
Thanks!

foo.py (3.38 KB)

I figured it out. You don’t need to send messages or call beginBatch or anything. Simply do this:

    current, new = (self.grid.GetNumberRows(), len(self.all_instances))
   
    if new < current:
        #- Delete rows:
        self.grid.DeleteRows(0, current-new, True)

    if new > current:
        #- append rows:
        self.grid.AppendRows(new-current)

It seems to work just fine.
-alex-

···

On Wednesday, July 3, 2013 1:56:47 PM UTC-4, Alexander Gray II wrote:

Unfortunately this is yet another grid resize question.
I’ve attached a foo.py that demonstrates my problem.
When the refresh button is pressed, the data (just a simple python list) gets modified (grows or shrinks).

I’m trying to get the grid to re-create itself (or update itself) when this happens.

I’m following the example here: http://wiki.wxpython.org/wxGrid
But for some reason the number of rows just keeps growing.

I suspect the bug lies in this part of the code:
/// myDataList has just been modified (randomly added or removed rows), so now it’s time to update the grid to display this new data:

    self.grid.BeginBatch()
    current, new, delmsg, addmsg = (len(self.myDataList), self.grid.GetNumberRows(), wxGRIDTABLE_NOTIFY_ROWS_DELETED, wxGRIDTABLE_NOTIFY_ROWS_APPENDED)
    if new < current:
            print 'deleting rows:', current-new
            msg = wx.grid.GridTableMessage(
                    self.grid.GetTable(),
                    delmsg,
                    new,    # position
                    current-new,
            )
            self.grid.ProcessTableMessage(msg)
    elif new > current:
            print 'adding rows: ', new-current
            msg = wx.grid.GridTableMessage(
                    self.grid.GetTable(),
                    addmsg,
                    new-current
            )
            self.grid.ProcessTableMessage(msg)
   
    msg = wx.grid.GridTableMessage(self.grid.GetTable(), wxGRIDTABLE_REQUEST_VIEW_GET_VALUES)
    self.grid.ProcessTableMessage(msg)
    self.grid.EndBatch()

Any ideas what could be wrong? It’s driving me nuts!
Thanks!