Changing tables in a wxGrid

The documentation on wxPyGridTableBase says "you cannot call SetTable more
than once for a given grid. If you need to change tables, you'll need to
create a new wxGrid object." This is what I'm doing, but the old grid is
still displayed, and the new grid does not appear. The documentation on
this in wxWindows and wxPython is a bit inconsistent (see below) and I'm
pretty puzzled at this point.

The way I do things is as follows:

The class FactPanel is subclassed from wxPanel. It contains a sizer and a
method, showGrid(), that creates the grid.

The showGrid() method calls FactGrid() to create the grid and add it to the
sizer in the FactPanel. It then does a Fit(), SetSizeHints() and Show().

FactGrid is a class subclassed from wxGrid which calls FactTable() to
create the wxPyGridTableBase. It then calls SetTable() to set the table
for the grid.

Within the application, a wxFrame (FactFinder) is created and it adds a
FactPanel to one of its sizers. At a later point, the showGrid() method of
the FactPanel is called to create the table, and the grid, and display
this. This works great the first time.

When I want to replace the currently displayed grid with a new one, I
simply call the showGrid() method again to create the new grid (which, as
indicated above, also adds it to the FactPanel sizer). I then call Update
() on the FactPanel. But the result is that the old grid remains where it
was and nothing else seems to happen. I've tried various (and pretty
random) calls to things like Fit(), Show(), Update(), etc., but with no
results. My latest attempt has been to add a Remove() call in showGrid()
to remove the old FactGrid, and then to call Layout(). This at least has
*some* effect: the new column *headings* are displayed, but the grid data
is blank -- unless I then click within the grid, at which point the *old*
data starts to appear!!!

Now for the inconsistency ... I thought maybe I should use Clear() to wipe
out the grid data. In the wxWindows documentation for wxGrid, it says that
Clear() won't work unless you override wxGridTableBase::Clear. Okay, but
override it *how*? And in addition, the discussion of wxPyGridTableBase in
the wiki about such methods as Clear() says "I would suggest not
implementing these methods, and instead call the appropriate methods on the
grid to reflect changes in your table". So what is one to do?

What am I missing here and what do I need to do to get the old displayed
table to go away and the new one to be displayed?

···

--------------------------------------
Gary H. Merrill
Director and Principal Scientist, New Applications
Data Exploration Sciences
GlaxoSmithKline Inc.
(919) 483-8456

The documentation on wxPyGridTableBase says "you cannot call SetTable more
than once for a given grid. If you need to change tables, you'll need to
create a new wxGrid object."

Another way that is usually done for wxPython apps is to use the same wxGrid and the same wxPyGridTableBase-derived class, but have the table be just a proxy for some other table class and then switch it to proxy for your other table when you want to change things.

This is what I'm doing, but the old grid is
still displayed, and the new grid does not appear. The documentation on
this in wxWindows and wxPython is a bit inconsistent (see below) and I'm
pretty puzzled at this point.

If you do create a new wxGrid then you need to Destroy() the old one to remove it, and then probably do a Layout on your parent window so it will resize the new one as needed.

···

gary.h.merrill@gsk.com wrote:

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