Setting Datagrid Table Column Headings

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cmalumphy
Posts: 5
Joined: Mon Dec 05, 2011 5:33 pm

Setting Datagrid Table Column Headings

Post by cmalumphy » Sun Feb 23, 2014 5:15 pm

I am having trouble dynamically setting column headings on a datagrid table that I populate with the results of a database query. The sql query is entered by the user into a textfield, so the column headings cannot be set in advance since each query could request different fields in different orders. The query runs and the datagrid is populated, but I have not been able to figure out how to set the column headings. Here is my code:

Code: Select all

on mouseUp
   -- check the global connection ID to make sure we have a database connection
   global gConnectionID
   if gConnectionID is not a number then
      answer error "Please connect to the database first."
      exit to top
   end if
   
   put field SQLField into tSQL  
   put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData
   
   //Couldn't even set columns successfully in advance
   //put "Name,Attempts,Success" into theFields
   //replace "," with CR in theFields
   //answer theFields
   //set the dgProp ["columns"] of group "DataGrid1" of card 3 to theFields
   
   -- check the result and display the data or an error message
   if item 1 of tData = "revdberr" then
      answer error "There was a problem querying the database:" & cr & tData
   else
      put tData into field "Data"
      put tData into field "TableField1" of card 2
      
      --tried this but it didn't work
      --put revDatabaseColumnNames(tData) into theFields -- get field names from table
      --replace "," with CR in theFields
      --set the dgProp["columns"] of group "DataGrid1" of card 3 to theFields

      --put tData into field "DataGrid1" of card ID 1078
      put false into firstLineContainsColumnNames
      set the dgText [firstLineContainsColumnNames ] of group "DataGrid1" of card 3 to tData
      
   end if
end mouseUp
Any suggestions would be appreciated.

Klaus
Posts: 13823
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Setting Datagrid Table Column Headings

Post by Klaus » Sun Feb 23, 2014 5:29 pm

Hi cmalumphy,

you did not provide TABLE and gConnectionID to the "revDatabaseColumnNames" function!
Try this:

Code: Select all

...
  else
      put tData into field "Data"
      put tData into field "TableField1" of card 2
  
      put revDatabaseColumnNames(gConnectionID,"Name of your table here!") into theFields -- get field names from table
      replace "," with TAB in theFields
      set the dgText[TRUE ] of group "DataGrid1" of card 3 to (theFields & CR & tData)
...
Best

Klaus

cmalumphy
Posts: 5
Joined: Mon Dec 05, 2011 5:33 pm

Re: Setting Datagrid Table Column Headings

Post by cmalumphy » Sun Feb 23, 2014 6:51 pm

It still doesn't work. The datagrid does not populate using your code and the column headings do not appear.

Code: Select all

      put revDatabaseColumnNames(gConnectionID,"mytable") into theFields -- get field names from table
      replace "," with TAB in theFields
      set the dgText[TRUE ] of group "DataGrid1" of card 3 to (theFields & CR & tData)
The following populates the datagird, but leaves the column names blank.

Code: Select all

    put revDatabaseColumnNames(gConnectionID,"mytable") into theFields -- get field names from table
    replace "," with TAB in theFields
    put false into firstLineContainsColumnNames
    set the dgText [firstLineContainsColumnNams ] of group "DataGrid1" of card 3 to tData
Another problem will be that I do not want to get the field names from the "table", I want the field names from the actual data set returned, since the user could select fewer fields than are in the table or might join two or more tables to obtain the results. I've tried removing the table name, but it fails too.

Klaus
Posts: 13823
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Setting Datagrid Table Column Headings

Post by Klaus » Sun Feb 23, 2014 7:01 pm

Oh, yes, sorry, my fault!

-> dgText [dgText [firstLineContainsColumnNams ]
The list of row column names in "firstLineContainsColumnNams" will "only" manage the ORDER
of the data in the datagrid, in case the data supplied are NOT in the same order of the columns
of the datagrid, so it knows where to put what!

Try this:
...
put revDatabaseColumnNames(gConnectionID,"mytable") into theFields -- get field names from table
replace "," with CR in theFields
set the dgProp ["columns"] of group "DataGrid1" of card 3 to theFields
...
I want the field names from the actual data set returned, since the user could select fewer fields than are in the table or might join two or more tables to obtain the results.
I'm afraid this info is not part of the returned data!
So you may need to get the user-selected db fields from somewhere else in your workflow!
Depends on how the user can select them, where you need to "hook"!


Best

Klaus

cmalumphy
Posts: 5
Joined: Mon Dec 05, 2011 5:33 pm

Re: Setting Datagrid Table Column Headings

Post by cmalumphy » Sun Feb 23, 2014 7:58 pm

Still doesn't work. Again, neither the datagrid nor the column headings appear. Could there be something wrong with the settings for my datagrid.

Also what does the following mean
There are two forms of the revDatabaseColumnNames function. It can either be used to return the list of column names in a record set, or as of version 2.9, the list of columns in a given table.

To get the list of columns in a record set use a form like this:

get revDatabaseColumnNames(tRecordSetId)
Where do you get tRecordSetId?

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Setting Datagrid Table Column Headings

Post by MaxV » Tue Feb 25, 2014 12:41 pm

Look this scrip, the "Data" tab contains a datagrid that change column labels each time: http://livecodeshare.runrev.com/stack/7 ... administer
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Setting Datagrid Table Column Headings

Post by bogs » Tue Jan 16, 2018 11:36 pm

It only took me 4 years to see this Max, but I really like that sample :D
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”