Datagrid help

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
jesse
Posts: 205
Joined: Thu Nov 11, 2010 6:32 pm

Datagrid help

Post by jesse » Thu Nov 11, 2010 9:37 pm

I have began a little testing and using the code below this creates a query and returns the results to tData. I have
the result showing up in a multi line text box but want the results to be formatted in a datagrid. Can anyone
explain how I could go about doing that?


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

-- construct the SQL (this selects all the data from the specified table)
put the text of field "tBrand" into boo
put "[EVEREST_SOLESU].[dbo].[ITEMS]" into tTableName -- set this to the name of a table in your database
put "SELECT * FROM " & tTableName & " where brand = '" & boo & "'" into tSQL

-- query the database
put revDataFromQuery(tab, cr, gConnectionID, tSQL) into tData

-- 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"
end if
end mouseUp
Deving on WinVista Home Prem. SP2 32 bit. Using LiveCode 4.6.1 Pro Build 1392

jesse
Posts: 205
Joined: Thu Nov 11, 2010 6:32 pm

Re: Datagrid help

Post by jesse » Thu Nov 11, 2010 10:20 pm

Nevermind I found out from here: http://lessons.runrev.com/spaces/lesson ... Grid-Array.

I did have one question though if anyone can help me. This lesson only explains how to manually set the fields to be displayed. How can I dynamically make all the fields show up based on the table. like SELECT * from table should show all fields in the table. Is the only way to make a field show up is manually add the column tot he datagrid?
Deving on WinVista Home Prem. SP2 32 bit. Using LiveCode 4.6.1 Pro Build 1392

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: Datagrid help

Post by Zryip TheSlug » Fri Nov 12, 2010 2:29 am

jesse wrote:Nevermind I found out from here: http://lessons.runrev.com/spaces/lesson ... Grid-Array.

I did have one question though if anyone can help me. This lesson only explains how to manually set the fields to be displayed. How can I dynamically make all the fields show up based on the table. like SELECT * from table should show all fields in the table. Is the only way to make a field show up is manually add the column tot he datagrid?
Jesse,

You can create dynamically the columns of a datagrid before to populate it.

1) First you need to get the names of the fields in the datagrid.

Assuming you have applied the lesson to your needs, you can use the variable theFields of the ConvertSQLCursorToArray handler.
Or you can write your own function:

Code: Select all

function fieldsFromCursor pCursor, pSeparator
  put revDatabaseColumnNames(pCursor) into tTheFieldsName
  if (pSeparator is not comma) then
      replace comma with pSeparator in tTheFieldsName
  end if
  return tTheFieldsName
end fieldsFromCursor
2) To create dynamically the DB fields in the datagrid, add this code before to populate the datagrid:

Code: Select all

set the dgProp["columns"] of group "datagrid 1" to fieldsFromCursor(pCursor, return)
or by using directly the variable "theFields" of the lesson:

Code: Select all

set the dgProp["columns"] of group "datagrid 1" to theFields
Note that the dgProp{"columns"] accepts names of columns separated by a return

3) Populate the datagrid:

Code: Select all

set the dgData of group "DataGrid 1" to theDataGridArray

Untested but should works.


Regards,
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

jesse
Posts: 205
Joined: Thu Nov 11, 2010 6:32 pm

Re: Datagrid help

Post by jesse » Wed Nov 17, 2010 5:43 pm

Thanks that helped get me in the right direction!
Deving on WinVista Home Prem. SP2 32 bit. Using LiveCode 4.6.1 Pro Build 1392

Post Reply