Hi, all
I am using wxGrid to display the content of a database in my app. Because we have a lot of data in the database, it's a large table. And I have two issues:
1) I find that It is quite slow to just override GetValue() method. Because every call to GetValue() invokes a database query. And when I scroll the grid, tens of cells need to be displayed, and GetValue() need to be called many times, and the screen flickers. I think the performance can be improved by getting values of all visible cells once. But I do not know how to do it. I guess that maybe there is a "standard" way to do this.
2) How to make the ww.Grid automatically update when the content of database changes? For example, when new data are add, then how to make the grid automatically add new rows to display them? I tried AppendRows(), but it doesn't work!
This is my code, any help would be appreciated!
···
#=============================================================================
class HugeTable(PyGridTableBase):
def __init__(self):
PyGridTableBase.__init__(self)
self.__database_ = wx.GetApp().database_
def GetNumberRows(self):
count = self.__database_.getRecordCount()
return count
def GetNumberCols(self):
return 4
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
# 1)It's quite slow to get cell values and display them one by one, if it is possible to
# get values of all current visible cells once from database and then display,
# it may be much fater, but I do not know how to.
value = self.__database_.getValue(row, col)
return value
def SetValue(self, row, col, value):
# do nothing
pass
def GetNumberRows(self, numRow):
# I do not know what should be filled here, is "pass" OK?
pass
class DataViewerGrid(Grid):
def __init__(self, parent):
Grid.__init__(self, parent, -1)
self.SetRowLabelSize(0)
table = HugeTable()
self.SetTable(table, True)
self.EnableEditing(False)
def onDataChange(self):
# 2)When database changes, how to update the grid? When the database changes,
# the grid don't.
# I have tried AppendRows():
# self.AppendRows(self.__database_.getRecordCount() - self.GetNumberRows())
# but the two methods always return the same value.
pass
--------------
bruce.who.hk
2006-08-23