Problem with AppendRows in wx.grid.Grid

Hi,

I am trying to build a search interface that displays the results of the search in a Grid. Every time the search is run, rows need to be added or deleted from the grid (based on the results of the previous search).

I used an example from the wxPython book to test adding and deleting rows and discovered that I am unable to append or delete rows from the grid when the grid is initialized using SetTable(). The code that I used is given below. I would appreciate it if anyone could let me know where I am going wrong and why I am unable to append rows to the grid.

====================Code Start============================
#!/usr/bin/env python

import wx
import wx.grid

class TestTable(wx.grid.PyGridTableBase):
    def __init__(self):
        wx.grid.PyGridTableBase.__init__(self)
        self.data = { (0,0) : "Here",
                        (1,1) : "is",
                        (2,2) : "some",
                        (3,3) : "data",
        }
               self.colLabels = ['one','two','three','four']
        self.rowLabels = ['R1', 'R2', 'R3', 'R4', 'R5']
               self.odd = wx.grid.GridCellAttr()
        self.odd.SetBackgroundColour("sky blue")
        self.odd.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
        self.even = wx.grid.GridCellAttr()
        self.even.SetBackgroundColour("sea green")
        self.even.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.BOLD))
           def GetNumberRows(self):
        #print len(self.data)
        return len(self.data)
       def GetNumberCols(self):
        return 4
       def IsEmptyCell(self, row, col):
        return self.data.get((row, col)) is not None
       def GetValue(self, row, col):
        value = self.data.get((row, col))
        if value is not None:
            return value
        else:
            return ''
       def GetColLabelValue(self, col):
        return self.colLabels[col]

    def GetRowLabelValue(self, row):
        return self.rowLabels[row]

    def SetValue(self, row, col, value):
        self.data[(row, col)] = value
           def GetAttr(self, row, col, kind):
        attr = [self.even, self.odd] [row % 2]
        attr.IncRef()
        return attr

    def AppendRows(self, numRows=1):
        print "apprvd"
        return len(self.data) <= 10

    def DeleteRows(self, pos=0, numRows=1):
        return True
   class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Grid Table", size =(300,400))
        self.grid = wx.grid.Grid(self)
               self.table = TestTable()
        self.grid.SetTable(self.table, True)

        self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK, self.OnRightClick)
           def OnRightClick(self, evt):
        print "Reached right click"
        self.grid.AppendRows(numRows=1)
        self.table.data = { (0,0) : "Here",
                        (1,1) : "is",
                        (2,2) : "changed",
                        (3,3) : "state",
                        (4,3) : "Julius",
                        (5,3) : "Caeser"
        }
        self.grid.ForceRefresh()
        print len(self.table.data)
        print self.grid.GetNumberRows()
       
frame = TestFrame()
frame.Show()
app.MainLoop()
====================Code End============================

Your help will be much appreciated.

Warm Regards
Feroze

···

===========================
Jar Jar Binks will be Jedi!!

Hi,

Feroze Arif wrote:

Hi,

I am trying to build a search interface that displays the results of the search in a Grid. Every time the search is run, rows need to be added or deleted from the grid (based on the results of the previous search).

I used an example from the wxPython book to test adding and deleting rows and discovered that I am unable to append or delete rows from the grid when the grid is initialized using SetTable(). The code that I used is given below. I would appreciate it if anyone could let me know where I am going wrong and why I am unable to append rows to the grid.

Have a look at the wiki, I think that should help you.

Particularly this page:
http://wiki.wxpython.org/index.cgi/UpdatingGridData?highlight=(grid)

Werner