Populating a datagrid

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Populating a datagrid

Post by MaxV » Thu Feb 13, 2014 1:41 pm

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?
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Populating a datagrid

Post by bangkok » Thu Feb 13, 2014 2:21 pm

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

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Populating a datagrid

Post by MaxV » Thu Feb 13, 2014 4:43 pm

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...
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply