From: "Robin Dunn" <robin@alldunn.com>
To: <wxpython-users@lists.wxwindows.org>
Subject: Re: [wxPython] How to make grid refresh with new gridTable?

> >
> > > I am working on a program that displays database tables in a
> > > grid.  I am utilizing a subclass of wxPyGridTableBase, named
> > > gridData, to setup a subclass of wxGrid, named grid.  I can get
> > > the first table to display properly by using
> > > grid.SetTable(gridData, true) within the grid.__init__ . Later,
> > > based on selection from a menu, I create a new instance of
> > > gridData in the menu's event handler and again, as in
> > > grid.__init__, I utilize grid.SetTable(gridData, true).
> >
> > Is it really a new set of data or would it make more sense to just update
> > the existing table?
>
> It's a new instance of wxPyGridTableBase, representing a new
> database table with new column headings, widths, and data.  There
> is no documentation so I don't know how to do what I need done.
>

I just had another look at the code and it looks like multiple calls to
SetTable for the same grid are not allowed, there is an assertion that would
be raised in debug mode and false is returned otherwise.  (There is also a
note in the code (from me!) stating that it should be possible to do but
wondering what the implications are.)

So what you'll want to do is to have only one instance of wxPyGridTableBase,
and just change the data within it.  Once that happens then you need to send
messages to the grid to notify it.  If the number of rows and columns stays
the same then it looks like all you need to send is
wxGRIDTABLE_REQUEST_VIEW_GET_VALUES, otherwise you'll need to send messages
to adjust the number of rows and columns and then the
wxGRIDTABLE_REQUEST_VIEW_GET_VALUES message.  Something like this (untested
pseudo code):


class MyTable(wxPyGridTableBase):

...

    def UpdateDataModel(self, newdata):
        # ... set new data ...
        self.GetView().BeginBatch()
        if newNumRows < curNumRows:
            msg = wxGridTableMessage(self,
                        wxGRIDTABLE_NOTIFY_ROWS_DELETED,
                        curNumRows - newNumRows,    # position
                        curNumRows - newNumRows)    # how many
            self.GetView().ProcessTableMessage(msg)
        if newNumRows > curNumRows:
            msg = wxGridTableMessage(self,
                        wxGRIDTABLE_NOTIFY_ROWS_APPENDED,
                        newNumRows - curNumRows)    # how many
            self.GetView().ProcessTableMessage(msg)

        # ... same thing for columns ....

        self.GetView().EndBatch()
        msg = wxGridTableMessage(self, wxGRIDTABLE_REQUEST_VIEW_GET_VALUES)
        self.GetView().ProcessTableMessage(msg)


Obviously it would make more sense to allow the table to be reset in an
existing grid, or at least have a single table message that would do all of
the above for you.  I've added it to my list to take a look at doing it, but
if anybody else is able to do it first then I am confident that the patch
would be accepted.

--
Robin Dunn
Software Craftsman
robin@AllDunn.com       Java give you jitters?
http://wxPython.org      Relax with wxPython!

UpdatingGridData (last edited 2008-03-11 10:50:28 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.