moving cell to cell in a datagrid

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dantomlin
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue Feb 26, 2008 4:07 pm

moving cell to cell in a datagrid

Post by dantomlin » Thu Feb 11, 2010 5:44 pm

Has anyone figured out how to move from cell to cell in a datagrid? For example, I have a datagrid where I want a user to enter data across 10 columns. After the user gets focus in one cell and enters data in that cell and hits enter or tab, I would like the focus to automatically move to the next cell (to the right) and be available to enter data again. This is similar to data entry in a spreadsheet (but the default in a spreadsheet is to move down) but if you highlight a number of cells to the right and then start entering, it moves to the right.

Any suggestions?

dantomlin
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue Feb 26, 2008 4:07 pm

Re: moving cell to cell in a datagrid

Post by dantomlin » Thu Feb 11, 2010 6:58 pm

actually figured something out, thanks to a big hint from trevor (who developed the datagrid).... my biggest problem was getting the column names but I wrote a switch event to get the next column based on the current one. I only have 10 columns, so it wasn't that bad. Not the perfect solution but it works... here is the code that I have attached to the datagrid group.

on CloseFieldEditor pFieldEditor
--get information from grid to save to database
put the dgDataOfIndex[the dgIndex of the target] of me into theDataA
put theDataA["bipadCode"] into tID
put the dgColumn of the target into tColumn
put the text of pFieldEditor into tNewData

--save data to database
put "directCopies" into tTable
SaveDataToDatabase tTable, tID, tColumn, tNewData

--update grid with data
put tNewData into theDataA[tColumn]
set the dgDataOfIndex[the dgIndex of the target] of me to theDataA

getNextColumnName(tColumn)
NextFieldToEdit
end CloseFieldEditor

-- this code moves the cursor to the next column
on NextFieldToEdit
global gColumn
put gColumn into theColumn
put the dgHilitedIndex of me into theIndex
EditCellOfIndex theColumn, theIndex
end NextFieldToEdit

--this code gets the name of the next column since I put specific column names to match my database table for updating
on getNextColumnName pColumn
global gColumn

switch pColumn
case "IG_allot"
put "IG_sale" into gColumn
break
case "IG_sale"
put "IPD_allot" into gColumn
break
case "IPD_allot"
put "IPD_sale" into gColumn
break
case "IPD_sale"
put "RV_allot" into gColumn
break
case "RV_allot"
put "RV_sale" into gColumn
break
case "RV_sale"
put "M_allot" into gColumn
break
case "M_allot"
put "M_sale" into gColumn
break
case "M_sale"
put "Subs" into gColumn
break
case "Subs"
put empty into gColumn
break
end switch
end getNextColumnName

--this code save the user's input into the database
command SaveDataToDatabase pTable, pID, pColumn, pData
global gHost, gDB, gDBUser, gDBPass

put revOpenDatabase("mysql", gHost, gDB, gDBUser, gDBPass) into tConID

if tConID is a number then
put "Select * from " & pTable into tSQL
put " where bipadCode='" & pID & "'" after tSQL
put revDataFromQuery(tab, return, tConID, tSQL) into tData

if tData is empty then
put "INSERT into " & pTable into tSQL
put " (bipadCode, " & pColumn & ")" after tSQL
put " VALUES ( '" & pID & "', " after tSQL
put " '" & pData & "')" after tSQL
--answer tSQL

revExecuteSQL tConID, tSQL
else
put "Update " & pTable into tSQL
put " Set " & pColumn after tSQL
put " = '" & pData & "'" after tSQL
put " where bipadCode='" & pID & "'" after tSQL
--answer tSQL

revExecuteSQL tConID, tSQL
end if

else
answer "database not connected"
end if

revCloseDatabase tConID
end SaveDataToDatabase

dantomlin
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue Feb 26, 2008 4:07 pm

Re: moving cell to cell in a datagrid

Post by dantomlin » Thu Feb 11, 2010 8:53 pm

added one more line of script, again thanks to Trevor, so that when the focus moves to the next field, the text is highlighted so a user modify any existing data without hitting delete or backspace. The code is just before editing the cell.

on NextFieldToEdit
global gColumn
put gColumn into theColumn
put the dgHilitedIndex of me into theIndex
set the dgTemplateFieldEditor["select text"] of the dgControl of me to true # this line highlights any existing data in the cell
EditCellOfIndex theColumn, theIndex
end NextFieldToEdit

dantomlin
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 43
Joined: Tue Feb 26, 2008 4:07 pm

Re: moving cell to cell in a datagrid

Post by dantomlin » Thu Feb 11, 2010 9:48 pm

One more thing...

if you having existing data in a cell and don't want to change it, you need this code (along with the other previously posted code). This is because if a cell is not change an "exitFieldEditor" is passed and without this, my previous cell movement stopped.... Now, you can just hit enter and the cursor will move cell to cell even if you don't change any data

on ExitFieldEditor pFieldEditor
put the dgColumn of the target into tColumn
getNextColumnName(tColumn)
NextFieldToEdit
end ExitFieldEditor

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: moving cell to cell in a datagrid

Post by trevordevore » Fri Feb 12, 2010 5:48 pm

Dan,

I think you might be able to generalize your getNextColumnName code a little bit. Using the (previously undocumented) property "visible columns" and the dgColumn property of the cell you can determine what the next column name is.

Code: Select all

function getNextColumnName
    local theColumn
    
    put the dgProp["visible columns"] of the dgControl of me into theColumns
    set the wholeMatches to true
    put lineOffset(the dgColumn of me, theColumns) into theLineNo
    if theLineNo > 0 then
        put line theLineNo of theColumns into theColumn
    end if
    
    return theColumn
end getNextColumnName
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

aetaylorBUSBnWt
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Sep 20, 2012 5:11 pm

Re: moving cell to cell in a datagrid

Post by aetaylorBUSBnWt » Sat Oct 22, 2022 4:20 pm

OK,

The above code assumes that the user always wants to go forward upon finishing editing a column.

now, how to find out if the user wants to go BACKWARDS - to the previous column because they used "shift tab key" ??

OR

they are done editing columns and hit the enter key or return key??

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”