Hello everyone,
I have the following code to fill up a wxListCtrl (with data from a
database), which can be used for every table in my database (as long as
the Tables structure is correctly set up).CODE:
self.table_list.ClearAll()
for r_index in range(len(data)):
for c_index in range(Tables[self.table]["numfields"]):
attr = getattr(data[r_index],
Tables[self.table]["fields"][c_index][0], "[error]")if r_index == 0:
self.table_list.InsertColumn(c_index,
Tables[self.table]["fields"][c_index][1].upper(),
width=Tables[self.table]["fields"][c_index][3])if c_index > 0:
self.table_list.SetStringItem(r_index, c_index,
Tables[self.table]["fields"][c_index][2] % attr)
else:
self.table_list.InsertStringItem(r_index,
Tables[self.table]["fields"][c_index][2] % attr)END OF CODE
The problem with this is that on the first row, only the first field
(r_index == 0, c_index == 0) is set, the other cols remain blank.
All further rows are correct.
Just guessing here, but notice that at the time you you call InsertStringItem
for your first row, the list contains exactly one column. The item that gets
created probably remembers that fact forever. The list control probably does
not retroactively inform all of its items when the number of columns changes.
Thus, you really SHOULD create all the columns before you start adding items.
When I move the InsertColumn statement to a loop of it's own (just after
the call to ClearAll), the problem goes away (but I loose some
efficiency).
I'm curious to know why you think you lose efficiency by moving the
InsertColumn call outside the loop. I suspect the reverse is true. As it
is, you are forcing the code to test the value of r_index and jump around the
InsertColumn for every cell in the entire table. By moving that call to a
separate "for" loop on columns outside of the row loop, thereby eliminating
the test, you reduce the size of your doubly-nested inner loop, and it should
actually run faster.
I might even be tempted to move the InsertStringItem outside of the column
loop, and make the column range be range(1,Tables[self.table...]).
self.table_list.ClearAll()
tbl = Tables[self.table]
for c_index in range(tbl["numfields"]):
self.table_list.InsertColumn(c_index,
tbl["fields"][c_index][1].upper(),
width=tbl["fields"][c_index][3])
for r_index in range(len(data)):
attr = getattr(data[r_index], tbl["fields"][0][0], "[error]")
self.table_list.InsertStringItem(r_index, tbl["fields"][0][2] % attr)
for c_index in range(1,tbl["numfields"]):
attr = getattr(data[r_index],
tbl["fields"][c_index][0], "[error]")
self.table_list.SetStringItem(r_index, c_index,
tbl["fields"][c_index][2] % attr)
···
On Thu, 13 Jun 2002 15:22:01 +0200, aderuwe@aqs-carcontrol.be (Alexander Deruwe) wrote:
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.