Possible solution for datagrid library errors

Bob Sneidar bobs at twft.com
Wed Nov 30 16:44:06 EST 2011


I use this:

on setTableColumns theDataGrid, theCardName
   if theDataGrid is empty then put "dgTableData" into theDataGrid -- now I can use it for any datagrid
   if theCardName is empty then put the name of this card into theCardName
   put the tblMaster of card theCardName into theTable
   put the tblDisplay of card theCardName into theColumnList
   if theColumnList is empty then 
      set the dgProp["columns"] of group theDataGrid to empty
      exit setTableColumns -- nothing more to do here
   end if

   set the itemdelimiter to tab
   repeat for each line theLine in theColumnList
      put item 1 of theLine & comma after theColumns
      put item 2 of theLine & comma after theLabels
      put item 3 of theLine & comma after theWidths
   end repeat
   delete the last char of theColumns
   replace comma with return in theColumns
   delete the last char of theLabels
   replace comma with return in theLabels
   delete the last char of theWidths
  
   -- datagrid needs to be empty here
   set the dgData of group theDataGrid to empty
   set the dgProp["columns"] of group theDataGrid to theColumns
   set the dgProp["column labels"] of group theDataGrid to theLabels
   set the dgProp["column widths"] of group theDataGrid to theWidths
end setTableColumns

Each card I want to use this function with has 2 properties: tblMaster and tblDisplay. tblMaster is the name of the sql table whose data I want to display. tblDisplay contains a tab delimited list of sql columns, labels and widths. These can be just a subset of the actual data contained in the datagrid array, but if there is a column name in the datagrid that matches a key in an array, then the data will be displayed by the datagrid. 

So I get all the columns of a table in a query and set the dgData of my datagrid to that, but only the columns in tblDisplay show any data. Get it? So now I can use the data in the datagrid as an ad hoc writable cursor complete with the primary key so I can easily build queries to update the data back to the sql table. 

Just so long as I clear the datagrid before setting anything with dgprops, I seem to be ok. Having a single datagrid makes development much easier because I don't have to worry about which datagrid I am dealing with at any given moment. Of course, I am only working with table datagrids, not forms. It could get ugly in a hurry if I were to try to customize a form datagrid for multiple things, so I agree with you there. 

Bob


On Nov 30, 2011, at 12:26 PM, Pete wrote:

> I've noticed a couple of posts recently related to changing the whole
> structure of a datagrid based on the type of data being displayed and I
> can't help feeling that's asking for trouble at some point (as Bob already
> discovered).
> 
> I'd like to throw out a couple of suggestions to handle this situation
> which I've implemented successfully.
> 
> One way is to include columns for all the different data types and
> show/hide the columns depending on the data type.  Extending that further,
> include as many datagrids on your card as you have data types and hide/show
> them as appropriate to the current data type.
> 
> Whether either of those will work for you or not depends on the number of
> different data types and data items you want to handle but I think they do
> run less risk of datagrid errors showing up.
> 
> Pete





More information about the use-livecode mailing list