Getting current record in Datagrid

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mpmahen
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jun 11, 2011 8:25 am

Getting current record in Datagrid

Post by mpmahen » Fri Jul 01, 2011 12:02 pm

Hi,

I'm using the datagrid with big tables in Table mode. I have set the dgNumberOfRecords to the record count of the table and have filled the datagrid with the data.
What I want to do is get the record data back when a user clicks a row. However because the grid is in table mode, directly connected to the table (sqlite) I do not seem to get anything back.

Any suggestions for this?

Maarten

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Getting current record in Datagrid

Post by townsend » Fri Jul 01, 2011 3:34 pm

Code: Select all

put the dgHilitedLines of group "myGrid" into line.number
put the dgDataOfLine[line.number] of group "myGrid" into line.array
put line.array["colName"] into cellContents

mpmahen
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jun 11, 2011 8:25 am

Re: Getting current record in Datagrid

Post by mpmahen » Sat Jul 02, 2011 6:54 am

Hi,

I tried this, but the line
put the dgDataOfLine[line.number] of group "DataGrid" into line.array
does not return any values. I have the code in a mouseup event in the datagrid group.
The line.number does get filled...

Maarten

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: Getting current record in Datagrid

Post by townsend » Sat Jul 02, 2011 5:51 pm

Try putting it in a button:

Code: Select all

on mouseUp
     put the dgHilitedLines of group "myGrid" into line.number
     if line.number > 0 then
          put the dgDataOfLine[line.number] of group "myGrid" into line.array
          put line.array["colName"] into cellContents
     else
          answer "First select (highlight) a line.)"
          exit to top
     end if
     -- do stuff with cellContents here
end mouseUp

mpmahen
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jun 11, 2011 8:25 am

Re: Getting current record in Datagrid

Post by mpmahen » Sun Jul 03, 2011 11:09 am

Hi,

I have tried all that, without luck. I have attached a small project showing the problem.
This one is a real head breaker and important for me. Any help is appreciated!

Maarten
Attachments
DBTest.zip
Sample project
(7.12 KiB) Downloaded 316 times

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Getting current record in Datagrid

Post by Klaus » Sun Jul 03, 2011 12:03 pm

Hi Maarten,

hmm, I am not sure what is going on here, but here some observations:

1. You do not set the DGTEXT or DGDATA of your datagrid in your scripts!
2. So the DGTEXT and DGDATA are EMPTY here!
3. You are setting "the dgNumberOfRecords of group "myGrid" instead, which is a special case!

This should be used when you have a LOT of records and want to mange the display manually.
Obviously this command fills the display but does not set the DGTEXT of DGDATA (yet)!

AND your are quering the "ID", but this is the "userID" that you need 8)

OK. here my modified card sand button cript, tested and works, read the comments:

Code: Select all

local sConnID
local sCursorID
local sRecordFields
local sRecordCount

## New! 
## Will hold the complete CONTET of your database query
local tContentArray

on closeCard
   CloseDatabase
end closeCard

## THis command is not used in oyur scripts!
command GetDataForLine pLine, @pOutData
   revMoveToRecord sCursorID, pLine - 1 
   put ConvertCurrentRowToArray() into pOutData
end GetDataForLine

## This function is also not used in your scripts!
function ConvertCurrentRowToArray
   local theArray
   repeat for each item theField in sRecordFields
      put revDatabaseColumnNamed(sCursorID,theField) into theArray[theField]
   end repeat
   return theArray
end ConvertCurrentRowToArray

command OpenDatabase
   put the filename of this stack into thePath
   set the itemdelimiter to slash
   put "Users.sqlite" into the last item of thePath
   put revOpenDatabase("sqlite",thePath,,,,) into sConnID
   if sConnID is not an integer then
      answer "Error connecting to the database:" && sConnID & "."
      put empty into sConnID
      exit to top
   end if
end OpenDatabase

command OpenCursor
   put revQueryDatabase(sConnID,"SELECT * FROM users") into sCursorID
   if sCursorID is not an integer then
      answer "Error opening cursor:" && sCursorID & "."
      exit to top
   end if

## Now we put the content of your db query "en bloc" into a variable (TAB & CR delimited list)
   put revDataFromQuery(TAB,CR,sConnID,"SELECT * FROM users") into tContentArray
end OpenCursor

command CloseCursor
   try
      revCloseCursor sCursorID
   catch e
   end try
end CloseCursor

command setRecords
   Local myRecords
   put revDatabaseColumnNames(sCursorID) into sRecordFields
   put sRecordFields into myRecords
   replace "," with CR in myRecords 
   set the dgProp["columns"] of group "myGrid" to myRecords

  ## Since your datagrid is a of kind TABLE, we can simply set its DGTEXT
   set the dgtext of grp "myGrid" to tContentArray

  ## No more necessary
   #   put revNumberOfRecords(sCursorID) into sRecordCount
   #   lock screen
   #   set the dgNumberOfRecords of group "myGrid" to sRecordCount
   #   unlock screen
end setRecords

## Button "Get record data"
on mouseUp
   put the dgHilitedLines of grp "myGrid" into line.number
   put the dgdata of grp "myGrid" into tArray1
   put the dgtext of grp "myGrid" into tArray2
   if line.number > 0 then
      put the dgDataOfLine[line.number] of group "myGrid" into line.array

     ## USERID!!!!!!
      put line.array["userID"] into cellContents
      put cellContents
   else
      answer "First select (highlight) a line."
      exit to top
   end if
   -- do stuff with cellContents here
end mouseUp
OK, this is only ONE way to do what you need.
Others might come with smarter solutions but this works :D


Best

Klaus

mpmahen
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 7
Joined: Sat Jun 11, 2011 8:25 am

Re: Getting current record in Datagrid

Post by mpmahen » Mon Jul 04, 2011 3:58 am

Thanks Klaus for the comments.

However I included a small database for demo puposes. Normally I want to work with big databases, that is why I set the sRecordCount.
(The column id was from a big table, forgot to name it UserID...)
The rest is straight from the example for big databases. However that uses a form datagrid with row behavior. When you set it to table you have no row behavior. According to the manual, if you set the recordcount property, the dgData etc. do not return anything... So how to get the pressed record data?

Maarten

Post Reply