Grid issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • scottmallory@gmail.com

    #1

    Grid issues

    Greetings,
    I am new to Python...and I am having some difficulty updating a grid
    (wx.grid.PyGrid TableBase). 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().U pdateRowData(nd ata, 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.gr id.PyGridTableB ase):

    def __init__(self, headers=(['h','h','h']),
    data=(['a','a','a'],['b','b','b'])):
    wx.grid.PyGridT ableBase.__init __(self)
    self.headers = headers
    self.data = data


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


    def GetNumberCols(s elf): # Called from
    __init__
    return len(self.header s)


    def GetColLabelValu e(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(sel f, 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.GridTab leMessage(self,
    wx.grid.GRIDTAB LE_NOTIFY_ROWS_ DELETED, len(self.data), 1)
    # self.GetView(). ProcessTableMes sage(msg)

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

    msg1 = wx.grid.GridTab leMessage(
    self,wx.grid.GR IDTABLE_NOTIFY_ ROWS_APPENDED, len(ndata) )
    msg2 = wx.grid.GridTab leMessage(
    self,wx.grid.GR IDTABLE_REQUEST _VIEW_GET_VALUE S )

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


    def UpdateRowData(s elf, ndata, row, col):
    num = self.GetNumberR ows()

    if row <= num:
    for i in range(col):
    #print ndata[i]
    sr = ndata[0][i]
    print i, sr
    self.SetValue(r ow, i, str(sr))
    #self.SetCellVa lue(row, i, str(sr)) ?????
    else:
    for i in ndata:
    self.data.appen d(i)

    msg1 = wx.grid.GridTab leMessage(
    self,wx.grid.GR IDTABLE_NOTIFY_ ROWS_APPENDED, len(ndata) )
    self.GetView(). ProcessTableMes sage(msg1)

    msg = wx.grid.GridTab leMessage(
    self,wx.grid.GR IDTABLE_REQUEST _VIEW_GET_VALUE S )
    self.GetView(). ProcessTableMes sage(msg)


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

    msg = wx.grid.GridTab leMessage(
    self,wx.grid.GR IDTABLE_REQUEST _VIEW_GET_VALUE S )
    self.GetView(). ProcessTableMes sage(msg)
    except IndexError:
    print "FAIL"
    #------------------------------ End DataTable Class

Working...