Discussion:
How to AppendRows() to wx.grid.Grid
Rich Shepard
2008-05-30 16:20:35 UTC
Permalink
I'm really puzzled because my code is not working as expected. In the
method I'm debugging are these lines:

self.dataGrid.ClearGrid()
nRows = self.table.GetNumberRows()
self.dataGrid.AppendRows(nRows - 1)
self.dataGrid.ForceRefresh()

When I walk through the method using winpdb, nRows is correctly assigned
the value of 114. (The grid is initialized with 1 row.) However, nothing
happens when 'self.dataGrid.AppendRows(nRows - 1)' is executed; therefore,
there's nothing to refresh.

Because the data are managed by a grid table, in the class
dataTable(wx.grid.PyGridTableBase) I created a method to approve the
addition of rows to the display grid:

def AppendRows(self, numRows=1):
return self.GetNumberRows() - 1

There must be something wrong with my syntax in either or both of these
two methods, but I cannot find the error(s) from the book or online docs. A
clue is appreciated.

Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
raffaello
2008-05-30 17:34:39 UTC
Permalink
wxGrid::AppendRows

*bool* *AppendRows*(*int **numRows = 1*, *bool **updateLabels = true*)

Appends one or more new rows to the bottom of the grid and returns true if
successful. The updateLabels argument is not used at present.
If you are using a derived grid table class you will need to override
wxGridTableBase::AppendRows<wx_wxgridtablebase.html#wxgridtablebaseappendrows>

This is simply copied from the documentation, but from your mail I
understand that you are really using a table, and if so
self.dataGrid.AppendRows does not work.
Post by Rich Shepard
I'm really puzzled because my code is not working as expected. In the
self.dataGrid.ClearGrid()
nRows = self.table.GetNumberRows()
self.dataGrid.AppendRows(nRows - 1)
self.dataGrid.ForceRefresh()
When I walk through the method using winpdb, nRows is correctly assigned
the value of 114. (The grid is initialized with 1 row.) However, nothing
happens when 'self.dataGrid.AppendRows(nRows - 1)' is executed; therefore,
there's nothing to refresh.
Because the data are managed by a grid table, in the class
dataTable(wx.grid.PyGridTableBase) I created a method to approve the
return self.GetNumberRows() - 1
There must be something wrong with my syntax in either or both of these
two methods, but I cannot find the error(s) from the book or online docs. A
clue is appreciated.
Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
503-667-8863
_______________________________________________
wxpython-users mailing list
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Rich Shepard
2008-05-30 17:51:10 UTC
Permalink
Post by raffaello
Appends one or more new rows to the bottom of the grid and returns true if
successful. The updateLabels argument is not used at present. If you are
using a derived grid table class you will need to override
wxGridTableBase::AppendRows<wx_wxgridtablebase.html#wxgridtablebaseappendrows>
Raffaello,

Done the above, as noted in my message.
Post by raffaello
This is simply copied from the documentation, but from your mail I
understand that you are really using a table, and if so
self.dataGrid.AppendRows does not work.
Oh? This is counter to what the book and on-line docs indicate. If my data
are stored in a database table and retrieved into a list of tuples, I cannot
dynamically size the table to accommodate all data rows? If that's the case,
I'm in a real bind because the grid widget wants to be initialized when the
application is invoked, and the size of the grid cannot be known then.

How do I size the grid to the number of tuples in the list?

Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
raffaello
2008-05-30 18:34:50 UTC
Permalink
In most of my applications I use a grid and, like you, at the beginning I
don't know how many rows will be filled, also because the underlying
database is dynamically updated by the user. You can surely increase or
reduce the number of rows in the grid after creation. The only problem, if
you use a wx.GridTableBase, is that you do not change the number of rows
directly through the same grid, but indirectly through the table with
self.grid.GetTable().AppendRows(), self.grid.GetTable().DeleteRows() and
even self.grid.GetTable().InsertRows().
By the way, if your data are only strings, dates and numbers (including
booleans) the recourse to a wx.GridTableBase is NOT necessary: just use
wx.grid, dynamically, with all its normal methods.
Post by raffaello
Appends one or more new rows to the bottom of the grid and returns true if
Post by raffaello
successful. The updateLabels argument is not used at present. If you are
using a derived grid table class you will need to override
wxGridTableBase::AppendRows<wx_wxgridtablebase.html#wxgridtablebaseappendrows>
Raffaello,
Done the above, as noted in my message.
This is simply copied from the documentation, but from your mail I
Post by raffaello
understand that you are really using a table, and if so
self.dataGrid.AppendRows does not work.
Oh? This is counter to what the book and on-line docs indicate. If my data
are stored in a database table and retrieved into a list of tuples, I cannot
dynamically size the table to accommodate all data rows? If that's the case,
I'm in a real bind because the grid widget wants to be initialized when the
application is invoked, and the size of the grid cannot be known then.
How do I size the grid to the number of tuples in the list?
Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
503-667-8863
_______________________________________________
wxpython-users mailing list
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Rich Shepard
2008-05-30 19:10:09 UTC
Permalink
Post by raffaello
In most of my applications I use a grid and, like you, at the beginning I
don't know how many rows will be filled, also because the underlying
database is dynamically updated by the user. You can surely increase or
reduce the number of rows in the grid after creation. The only problem, if
you use a wx.GridTableBase, is that you do not change the number of rows
directly through the same grid, but indirectly through the table with
self.grid.GetTable().AppendRows(), self.grid.GetTable().DeleteRows() and
even self.grid.GetTable().InsertRows().
Raffaello,

How interesting! I did not notice grid.GetTable() before. Unfortunately,
it's still not working for me; no rows appended.
Post by raffaello
By the way, if your data are only strings, dates and numbers (including
booleans) the recourse to a wx.GridTableBase is NOT necessary: just use
wx.grid, dynamically, with all its normal methods.
So, I can still keep data in the SQLite database table, but use
CreateGrid() instead of SetTable()? I'll re-write the module and see if that
solves my problem.

Thanks,

Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
raffaello
2008-05-30 19:19:11 UTC
Permalink
Yes, you can: And the problem of adding rows should be solved.
Post by raffaello
In most of my applications I use a grid and, like you, at the beginning I
Post by raffaello
don't know how many rows will be filled, also because the underlying
database is dynamically updated by the user. You can surely increase or
reduce the number of rows in the grid after creation. The only problem, if
you use a wx.GridTableBase, is that you do not change the number of rows
directly through the same grid, but indirectly through the table with
self.grid.GetTable().AppendRows(), self.grid.GetTable().DeleteRows() and
even self.grid.GetTable().InsertRows().
Raffaello,
How interesting! I did not notice grid.GetTable() before. Unfortunately,
it's still not working for me; no rows appended.
By the way, if your data are only strings, dates and numbers (including
Post by raffaello
booleans) the recourse to a wx.GridTableBase is NOT necessary: just use
wx.grid, dynamically, with all its normal methods.
So, I can still keep data in the SQLite database table, but use
CreateGrid() instead of SetTable()? I'll re-write the module and see if that
solves my problem.
Thanks,
Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
503-667-8863
_______________________________________________
wxpython-users mailing list
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Rich Shepard
2008-05-30 19:49:33 UTC
Permalink
Post by raffaello
Yes, you can: And the problem of adding rows should be solved.
I'll let you know when I have rewritten the module and tested it.

Thinking about this, it makes sense that we don't need the grid table
because the data are extracted into a list of tuples and the wx.grid.Grid
doesn't know the origin of those values. Similarly, added data are inserted
into the database table by the middleware and the grid's not involved in
that transaction, either. I never saw it this way until you pointed it out
for me.

Again, thanks,

Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
Rich Shepard
2008-05-30 21:54:01 UTC
Permalink
Post by raffaello
And the problem of adding rows should be solved.
Rafaello,

That it is. Thank you very much.

Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
raffaello
2008-05-30 22:05:37 UTC
Permalink
Don't mention it.
I hope that Robin is not reading us, but the more I delve into wx.grid the
less I see the marginal utility of the tablebase.
Post by raffaello
And the problem of adding rows should be solved.
Rafaello,
That it is. Thank you very much.
Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
503-667-8863
_______________________________________________
wxpython-users mailing list
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Frank Millman
2008-05-31 05:56:47 UTC
Permalink
Post by raffaello
Don't mention it.
I hope that Robin is not reading us, but the more I delve
into wx.grid the less I see the marginal utility of the tablebase.
IMHO there are several uses -

1. If your data is very large - see the demo for an example of a grid
consisting of 10000 columns x 10000 rows. Response is instant. If you tried
this with a normal grid it would be much slower.

2. If you do not want all columns or rows to be displayed. You can play
tricks with setting row heights or column widths to zero, but if there are a
lot of them, or they change dynamically, it can be more effective to
maintain a mapping between the actual data and the visible data, and use
that to determine what to actually display.

3. If you have an indirect link between the actual data and the data to
display. I use this technique throughout my system, as I use a grid to
display the entire contents of a database table. I want my system to be
scalable, so the table may have 20 rows or 20 million rows. I do not read in
the entire contents of the table up front, I open a database cursor, and
'fetch' the data required in the GetValue() method of the tablebase. It
works very well.

In response to Rich's original question, this is how I append rows to a grid
-

msg = wx.grid.GridTableMessage(self.tablebase,
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED,noRows)
self.ProcessTableMessage(msg)

where self is an instance of wx.grid.Grid.

I just had a look at the docs to quote you a reference, but I cannot see
where I got this information from - it has been working for years without
any problems. You can find an example of this in demo/GridCustTable.py.

HTH

Frank Millman
raffaello
2008-05-31 06:38:09 UTC
Permalink
Frank,
Since the first time I saw the Three Days of the Condor (so you can guess
how old I am) I was stunned by the possibilities of the database. You are
telling that a tablebase is much faster, but a quarter of a second is fast
enough for my reflexes, and I love the flexibility of the MySQL queries: If
I had to write myself in C an equivalent code, that would be slow indeed.

So I think I'll stick to my dotard habits, but I thank you very much for
your kind explanation.
Post by Frank Millman
Post by raffaello
Don't mention it.
I hope that Robin is not reading us, but the more I delve
into wx.grid the less I see the marginal utility of the tablebase.
IMHO there are several uses -
1. If your data is very large - see the demo for an example of a grid
consisting of 10000 columns x 10000 rows. Response is instant. If you tried
this with a normal grid it would be much slower.
2. If you do not want all columns or rows to be displayed. You can play
tricks with setting row heights or column widths to zero, but if there are a
lot of them, or they change dynamically, it can be more effective to
maintain a mapping between the actual data and the visible data, and use
that to determine what to actually display.
3. If you have an indirect link between the actual data and the data to
display. I use this technique throughout my system, as I use a grid to
display the entire contents of a database table. I want my system to be
scalable, so the table may have 20 rows or 20 million rows. I do not read in
the entire contents of the table up front, I open a database cursor, and
'fetch' the data required in the GetValue() method of the tablebase. It
works very well.
In response to Rich's original question, this is how I append rows to a grid
-
msg = wx.grid.GridTableMessage(self.tablebase,
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED,noRows)
self.ProcessTableMessage(msg)
where self is an instance of wx.grid.Grid.
I just had a look at the docs to quote you a reference, but I cannot see
where I got this information from - it has been working for years without
any problems. You can find an example of this in demo/GridCustTable.py.
HTH
Frank Millman
_______________________________________________
wxpython-users mailing list
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users
Rich Shepard
2008-05-31 14:21:56 UTC
Permalink
Post by Frank Millman
In response to Rich's original question, this is how I append rows to a grid
-
msg = wx.grid.GridTableMessage(self.tablebase,
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED,noRows)
self.ProcessTableMessage(msg)
where self is an instance of wx.grid.Grid.
I just had a look at the docs to quote you a reference, but I cannot see
where I got this information from - it has been working for years without
any problems. You can find an example of this in demo/GridCustTable.py.
Frank,

Thank you. I missed this when I looked at the demo.

My data for the grid widget will not exceed a couple of hundred rows and,
perhaps, 50 columns in this application. However, I am saving this
information because I'll probably need it in the future.

Rich
--
Richard B. Shepard, Ph.D. | Integrity Credibility
Applied Ecosystem Services, Inc. | Innovation
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
Loading...