Greetings,
I am new to Python...and I am having some difficulty updating grid cells
(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
The class is below for your review, soory if it is too long, it's my first post.
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):
return len(self.data)
def GetNumberCols(self):
return len(self.headers)
def GetColLabelValue(self, col):
return self.headers[col]
def GetValue(self, row, col):
try:
return self.data[row][col]
except KeyError:
pass
def IsEmptyCell(self, row, col):
#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
# ~~~~~~~~~~~~~~~~
# It gets this far and works fine, then it fails
# ~~~~~~~~~~~~~~~~
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
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.306 / Virus Database: 266.4.0 - Release Date: 2/22/2005