Grid issues

scottmallory at gmail.com scottmallory at gmail.com
Tue Feb 22 16:42:02 EST 2005


Greetings,
I am new to Python...and I am having some difficulty updating a grid
(wx.grid.PyGridTableBase). Everything works just fine (adding
/deleting/ resizing rows, cols) except updating individual cells.

I am using the grids to display data from a database (which is working
fine), I am trying to use the SetValue (as well as self.data[r][c] =
string and others ) method but it does not seem to work. I am at a
lost.

Here is the call to the UpdateRowData function:

self.frame.grid_4.GetTable().UpdateRowData(ndata, ndata[0][0],
len(ndata[0]))

frame id wxFrame
ndata is a list
ndata[0][0] is the row number to update
len(ndata[0]) is the number of columns for the table

Here is the class for your review.

Any help would be most appreciated.
Scott




#-----------------------------------------------------------------
class DataTable(wx.grid.PyGridTableBase):

    def __init__(self, headers=(['h','h','h']),
data=(['a','a','a'],['b','b','b'])):
        wx.grid.PyGridTableBase.__init__(self)
        self.headers = headers
        self.data = data


    def GetNumberRows(self):                            #  Called from
__init__
        return len(self.data)


    def GetNumberCols(self):                            #  Called from
__init__
        return len(self.headers)


    def GetColLabelValue(self, col):                    #  Called from
__init__
        return self.headers[col]


    def GetValue(self, row, col):                        # Called from
__init__
        # Get Value is for updating the Table on screen
        try:
            return self.data[row][col]
        except KeyError:
            pass

    def IsEmptyCell(self, row, col):                     # Called from
__init__
        #print "empty Cell", row, col
        try:
            if self.data[row][col] != "":
                return True
            else:
                return False
        except:
            return False

#--------- END __INIT__ Calls


    #def RemoveData(self,rowNum):
    #    self.data.pop()
    #    msg = wx.grid.GridTableMessage(self,
wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED,  len(self.data),  1)
    #    self.GetView().ProcessTableMessage(msg)

    def AddData(self, ndata):
        #print "Add Data"
        for i in ndata:
            self.data.append(i)

        msg1 = wx.grid.GridTableMessage(
self,wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, len(ndata) )
        msg2 = wx.grid.GridTableMessage(
self,wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES )

        for msg in [ msg1, msg2]:
            self.GetView().ProcessTableMessage(msg)


    def UpdateRowData(self, ndata, row, col):
        num = self.GetNumberRows()

        if row <= num:
            for i in range(col):
                #print ndata[i]
                sr = ndata[0][i]
                print i, sr
                self.SetValue(row, i, str(sr))
                #self.SetCellValue(row, i, str(sr))  ?????
        else:
            for i in ndata:
                self.data.append(i)

            msg1 = wx.grid.GridTableMessage(
self,wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, len(ndata) )
            self.GetView().ProcessTableMessage(msg1)

        msg = wx.grid.GridTableMessage(
self,wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES )
        self.GetView().ProcessTableMessage(msg)


    def SetValue(self, row, col, value):
        try:
            self.data[row][col] = value

            msg = wx.grid.GridTableMessage(
self,wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES )
            self.GetView().ProcessTableMessage(msg)
        except IndexError:
            print "FAIL"
#------------------------------ End DataTable Class




More information about the Python-list mailing list