Page 1 of 1

Populating a datagrid

Posted: Thu Feb 13, 2014 1:41 pm
by MaxV
Hello,
I use the following code to retrieve all a data from any type of table in a database and show it in a datagrid:

Code: Select all

on populatedatagrid
   # curtab contains table name
   #let's clean datagrid
   set the dgData of group "myDatagrid" to empty
   #connection
   put the connID of this stack   into connID       
   #now starts the real code
   #get column names
   put revDatabaseColumnNames(connID, curtab ) into titles
   replace "," with return in titles
   #let's add the  rowid in order to get unique row for further actions
   put  "rowid" & return before titles
   set the dgProp["columns"]  of group "myDatagrid" to titles
   #let's recover all data from the table in database
    put "SELECT rowid,* FROM " & curtab  into tSQL
    put revDataFromQuery(tab,return,connID,tSQL) into tRecords
    set the dgtext of group "myDatagrid" to tRecords                        
end populatedatagrid
The code works, but if any cell contains a return char (LF, CR or LFCR), the datagrid shows more (wrong) rows. This is due to the fact that I use dgText property to populate the datagrid.

I suppose that I could avoid it using the dgData property, but how can I get an array from a query and use it in a datagrid?

Re: Populating a datagrid

Posted: Thu Feb 13, 2014 2:21 pm
by bangkok
2 ways :

-you clean the text using weird characters as delimiters :

Code: Select all

    put revDataFromQuery("\","|",connID,tSQL) into tRecords
replace cr with " " in tRecords
replace lf with " " in tRecords
replace tab with " " in tRecords
replace "\" with tab in tRecords
replace "|" with cr in tRecords
set the dgtext of group "myDatagrid" to tRecords
-you use arrays

Like :
myArray[1]["Col A"]
myArray[1]["Col B"]
myArray[2]["Col A"]
myArray[2]["Col B"]

With a set of loops, you put the content of tRecords in such an array.
and then :

Code: Select all

set the dgData of group "myDatagrid" to myArray

Re: Populating a datagrid

Posted: Thu Feb 13, 2014 4:43 pm
by MaxV
Hi Bangkok,
your first solution is simple, but there is no way to predict which combination of char is not in a cell.
I'm reading about the revQueryDatabase and searching on internet I just found this: http://lessons.runrev.com/s/lessons/m/4 ... grid-array

This should be the best solution...