Getting current record in Datagrid
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Getting current record in Datagrid
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
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
Re: Getting current record in Datagrid
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
Re: Getting current record in Datagrid
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
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
Re: Getting current record in Datagrid
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
Re: Getting current record in Datagrid
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
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
Re: Getting current record in Datagrid
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
OK. here my modified card sand button cript, tested and works, read the comments:
OK, this is only ONE way to do what you need.
Others might come with smarter solutions but this works
Best
Klaus
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

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
Others might come with smarter solutions but this works

Best
Klaus
Re: Getting current record in Datagrid
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
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