== Introduction == Gives a way to append a line into a Grid table used with a Grid. == What Objects are Involved == wxgrid, wxgridtablebase == Process Overview == There are plenty of sample in the wxPy demo showing how to create/modify a Grid, but not to show how to insert/append a row. This is particularly difficult in the case of the Grid table as the appenrows/insertrows functions does not work. This sample processes by destroying and creating again the Grid. As the refresh method does not work on Windows XP, a workaround is got by getting and setting the size. == Special Concerns == The sample is based on gridcusttable.py from the demo. == Code Sample == {{{ #!python from wxPython.wx import * from wxPython.grid import * #--------------------------------------------------------------------------- class CustomDataTable(wxPyGridTableBase): def __init__(self,data): self.data= data wxPyGridTableBase.__init__(self) self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform', 'Opened?', 'Fixed?', 'Tested?', 'TestFloat'] self.dataTypes = [wxGRID_VALUE_NUMBER, wxGRID_VALUE_STRING, wxGRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical', wxGRID_VALUE_NUMBER + ':1,5', wxGRID_VALUE_CHOICE + ':all,MSW,GTK,other', wxGRID_VALUE_BOOL, wxGRID_VALUE_BOOL, wxGRID_VALUE_BOOL, wxGRID_VALUE_FLOAT + ':6,2', ] def GetNumberRows(self): return len(self.data) + 1 def GetNumberCols(self): return len(self.data[0]) def IsEmptyCell(self, row, col): try: return not self.data[row][col] except IndexError: return true def GetValue(self, row, col): try: return self.data[row][col] except IndexError: return '' def SetValue(self, row, col, value): try: self.data[row][col] = value except IndexError: self.data.append([''] * self.GetNumberCols()) self.SetValue(row, col, value) msg = wxGridTableMessage(self, # The table wxGRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it 1) # how many self.GetView().ProcessTableMessage(msg) def GetColLabelValue(self, col): return self.colLabels[col] def GetTypeName(self, row, col): return self.dataTypes[col] def CanGetValueAs(self, row, col, typeName): colType = self.dataTypes[col].split(':')[0] if typeName == colType: return true else: return False def CanSetValueAs(self, row, col, typeName): return self.CanGetValueAs(row, col, typeName) #--------------------------------------------------------------------------- class CustTableGrid(wxGrid): def __init__(self, parent, data): wxGrid.__init__(self, parent, -1) self.table = CustomDataTable(data) self.SetTable(self.table, true) self.SetRowLabelSize(0) self.SetMargins(0,0) self.AutoSizeColumns(False) EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick) def OnLeftDClick(self, evt): if self.CanEnableCellControl(): self.EnableCellEditControl() #--------------------------------------------------------------------------- class TestFrame(wxFrame): def __init__(self, parent): wxFrame.__init__(self, parent, -1, "Custom Table, data driven Grid Demo", size=(640,480)) self.data = [ [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12], [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50], [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56] ] self.initwin() def initwin(self): self.ref= None self.p = wxPanel(self, -1, style=0) b = wxButton(self.p, -1, "Add line...") b.SetDefault() EVT_BUTTON(self, b.GetId(), self.OnButton) self.bs = wxBoxSizer(wxVERTICAL) self.grid = CustTableGrid(self.p,self.data) self.bs.Add(self.grid, 1, wxGROW|wxALL, 5) self.bs.Add(b) self.p.SetSizer(self.bs) def OnButton(self, evt): self.data.append([1010, "Added line", "major", 1, 'MSW', 1, 1, 1, 1.12]) sz= self.p.GetSize() self.p.Destroy() self.initwin() self.p.SetSize(sz) print "button selected" #------------------------------------------------------------------------------- class mApp(wxApp): def OnInit(self): frame= TestFrame(None) frame.Show(true) return true #------------------------------------------------------------------------------- app = mApp(0) app.MainLoop() #------------------------------------------------------------------------------- }}} === Comments ===