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

   1 from wxPython.wx import *
   2 from wxPython.grid import *
   3 #---------------------------------------------------------------------------
   4 class CustomDataTable(wxPyGridTableBase):
   5     def __init__(self,data):
   6         self.data= data
   7         wxPyGridTableBase.__init__(self)
   8         self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
   9                           'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
  10         self.dataTypes = [wxGRID_VALUE_NUMBER,
  11                           wxGRID_VALUE_STRING,
  12                           wxGRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
  13                           wxGRID_VALUE_NUMBER + ':1,5',
  14                           wxGRID_VALUE_CHOICE + ':all,MSW,GTK,other',
  15                           wxGRID_VALUE_BOOL,
  16                           wxGRID_VALUE_BOOL,
  17                           wxGRID_VALUE_BOOL,
  18                           wxGRID_VALUE_FLOAT + ':6,2',
  19                           ]
  20     def GetNumberRows(self):
  21         return len(self.data) + 1
  22     def GetNumberCols(self):
  23         return len(self.data[0])
  24     def IsEmptyCell(self, row, col):
  25         try:
  26             return not self.data[row][col]
  27         except IndexError:
  28             return true
  29     def GetValue(self, row, col):
  30         try:
  31             return self.data[row][col]
  32         except IndexError:
  33             return ''
  34     def SetValue(self, row, col, value):
  35         try:
  36             self.data[row][col] = value
  37         except IndexError:
  38             self.data.append([''] * self.GetNumberCols())
  39             self.SetValue(row, col, value)
  40             msg = wxGridTableMessage(self,                             # The table
  41                                      wxGRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
  42                                      1)                                # how many
  43             self.GetView().ProcessTableMessage(msg)
  44     def GetColLabelValue(self, col):
  45         return self.colLabels[col]
  46     def GetTypeName(self, row, col):
  47         return self.dataTypes[col]
  48     def CanGetValueAs(self, row, col, typeName):
  49         colType = self.dataTypes[col].split(':')[0]
  50         if typeName == colType:
  51             return true
  52         else:
  53             return False
  54     def CanSetValueAs(self, row, col, typeName):
  55         return self.CanGetValueAs(row, col, typeName)
  56 #---------------------------------------------------------------------------
  57 class CustTableGrid(wxGrid):
  58     def __init__(self, parent, data):
  59         wxGrid.__init__(self, parent, -1)
  60         self.table = CustomDataTable(data)
  61         self.SetTable(self.table, true)
  62         self.SetRowLabelSize(0)
  63         self.SetMargins(0,0)
  64         self.AutoSizeColumns(False)
  65         EVT_GRID_CELL_LEFT_DCLICK(self, self.OnLeftDClick)
  66     def OnLeftDClick(self, evt):
  67         if self.CanEnableCellControl():
  68             self.EnableCellEditControl()
  69 #---------------------------------------------------------------------------
  70 class TestFrame(wxFrame):
  71     def __init__(self, parent):
  72         wxFrame.__init__(self, parent, -1, "Custom Table, data driven Grid  Demo", size=(640,480))
  73         self.data = [
  74             [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
  75             [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
  76             [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
  77             ]
  78         self.initwin()
  79     def initwin(self):
  80         self.ref= None
  81         self.p = wxPanel(self, -1, style=0)
  82         b = wxButton(self.p, -1, "Add line...")
  83         b.SetDefault()
  84         EVT_BUTTON(self, b.GetId(), self.OnButton)
  85         self.bs = wxBoxSizer(wxVERTICAL)
  86         self.grid = CustTableGrid(self.p,self.data)
  87         self.bs.Add(self.grid, 1, wxGROW|wxALL, 5)
  88         self.bs.Add(b)
  89         self.p.SetSizer(self.bs)
  90     def OnButton(self, evt):
  91         self.data.append([1010, "Added line", "major", 1, 'MSW', 1, 1, 1, 1.12])
  92         sz= self.p.GetSize()
  93         self.p.Destroy()
  94         self.initwin()
  95         self.p.SetSize(sz)
  96         print "button selected"
  97 #-------------------------------------------------------------------------------
  98 class mApp(wxApp):
  99     def OnInit(self):
 100         frame= TestFrame(None)
 101         frame.Show(true)
 102         return true
 103 #-------------------------------------------------------------------------------
 104 app = mApp(0)
 105 app.MainLoop()
 106 #-------------------------------------------------------------------------------

Comments

AppendingGridTable (last edited 2008-03-11 10:50:18 by localhost)

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