Grid Values: Getting and Setting

It seems backwards, yes, until you flip your mind around and realize that
these methods are meant to be overridden by you, and that from the Grid's
perspective, it is asking the Table object for the value, hence whatever
is returned by Table.GetValue() is what gets displayed in that cell by the
grid.

Paul,

   That's what I suspected, and it's nice to have my uncertainty erased.
Thank you.

You don't need to iterate the cells to get the values. Just override
Table.SetValue() and at that time write the value to the proper row and
field in the structure representing your database table.

   OK. The application uses SQLite3 as the backend with pysqlite2 as the
glue. In class dataTable() I now have:

   def SetValue(self, row, col, value):
     stmt = """INSERT or REPLACE into Data (comp, subcomp, var, curr1, curr2,
                                            curr3, curr4, curr5, curr6, curr7,
                                            curr8, curr9, curr10, curr11,
                                            curr12, noact, alt2, alt3, alt4,
                                            alt5, alt6, alt7, alt8, alt9, alt10,
                                            alt11, alt12, alt13, alt14, alt15,
                                            alt16, alt17, alt18
              values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,
              ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,
              ?,?)"""
     self.cur.execute(stmt)

   With other widgets, I follow the stmt with a list of fields holding the
values to be inserted/updated in the table. Does this look reasonable for
writing grid data?

Ditto, by the way, for getting the values from the database table. Override GetValue() to retrieve the value for your cell from the data structure.

   This I did already. It was going the other way that lost me.

Then when the user chooses to save(), you don't need to find anything from the grid at all: just iterate through your table representation and insert/update/delete as needed.

   Now the OnSave() function bound to the button calls dataTable.SetValue().

Again, thanks,

Rich

···

On Fri, 31 Aug 2007, Paul McNett wrote:

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc. | Accelerators(TM)
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

SetValue and GetValue "work" on one row/column and give you just that one
value, so you would only want to update the changed row/column and not all
of them in SetValue.

Werner,

   I saw that in the arguments passed to SetValue(), but do not grok how to
1) identify which value changed (or was newly added) and 2) write the sql
since the variable added/chaned could be different each time.

I would think that in OnSave() you would want to do dbconnection.commit()

   The commit() is issued after the write is completed.

Thanks,

Rich

···

On Sun, 2 Sep 2007, Werner F. Bruhin wrote:

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc. | Accelerators(TM)
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Hi Rich,

Rich Shepard wrote:

···

On Sun, 2 Sep 2007, Werner F. Bruhin wrote:

SetValue and GetValue "work" on one row/column and give you just that one
value, so you would only want to update the changed row/column and not all
of them in SetValue.

Werner,

  I saw that in the arguments passed to SetValue(), but do not grok how to
1) identify which value changed (or was newly added) and 2) write the sql
since the variable added/chaned could be different each time.

I keep a list ['sqlcolname1', 'sqlcolname2'] etc and the position in the list corresponds to the column number in the grid. I also keep column 0 reserved and it contains the primary key for the corresponding database row and that column is also in the grid but it is hidden to the user.

Hope this helps
Werner