Page 1 of 1

Datagrid help

Posted: Thu Nov 11, 2010 9:37 pm
by jesse
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

Re: Datagrid help

Posted: Thu Nov 11, 2010 10:20 pm
by jesse
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?

Re: Datagrid help

Posted: Fri Nov 12, 2010 2:29 am
by Zryip TheSlug
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,

Re: Datagrid help

Posted: Wed Nov 17, 2010 5:43 pm
by jesse
Thanks that helped get me in the right direction!