How can I edit a field in an arbitrary row in a DataGrid

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

How can I edit a field in an arbitrary row in a DataGrid

Post by dpatterson » Wed Jan 18, 2017 8:33 pm

First post here.
First, a quick bit about me. I'm new to LiveCode, but have been doing software architecture, design, and development professionally for well over 30 years so I have a pretty good understanding of the concepts. :)

I'm working on a LiveCode project that displays a modal with a DataGrid table.

If the user selects one or more rows in the DataGrid and clicks the OK button, the selected rows are copied to another DataGrid table in the main stack.

I want to open an edit field in the first of the copied rows in main other stack.

This is the code in my OK button:

Code: Select all

on mouseUp
   lock screen
   put the dgHiLitedLines of group selectComponentGrid into rowNumbers
   put the dgData of group selectComponentGrid into rows
   put true into firstTime
   repeat for each item rowNumber in rowNumbers
      put rows[rowNumber] into row 
      dispatch "AddData" to group bomGrid of card inventoryItem of stack inventory with row
      if firstTime is true then
         # Set focus to this row's quantity field.
         put "quantity" into colName
         put the result into lineNo -- the result contains the index of the new row
         send "EditCellOfIndex colName lineNo" to group bomGrid on card "inventoryItem" of stack "inventory"
         # At this point the result contains "no control exists for index column"
         put false into firstTime
      end if
      #end if
   end repeat
   unlock screen
   close this stack
end mouseUp
I've spent a number of hours reading the documentation, including the User Guide, and have looked at a number of posts. Still haven't been able to figure this out. I imagine it's quite simple once you get used to the LiveCode way of doing things.

If it's useful, I'm running LiveCode 9.0.0-dp4|Build 15003 Community Edition on Mac OS X 10.11.6.
TIA

sritcp
Posts: 368
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How can I edit a field in an arbitrary row in a DataGrid

Post by sritcp » Wed Jan 18, 2017 10:38 pm

In your code
.....
put "quantity" into colName
put the result into lineNo -- the result contains the index of the new row
send "EditCellOfIndex colName lineNo" to group bomGrid on card "inventoryItem" of stack "inventory"
.........

The 'send' command above will send "colName" and "lineNo" as the arguments for EditCellOfIndex, not the contents of the containers colName and lineNo.
Try
send "EditCellOfIndex" && colName & comma & lineNo to group ...........

I don't know if this will solve your problem, but should advance you to the next step.

Regards,
Sri

dpatterson
Posts: 24
Joined: Wed Jan 18, 2017 5:38 pm

Re: How can I edit a field in an arbitrary row in a DataGrid

Post by dpatterson » Wed Jan 18, 2017 11:44 pm

Sri,

Thanks!
I found that particular syntax in a post somewhere. It did seem a bit odd to me, but...

Oddly enough, while that one change did open a field for editing, it was not in the correct row.
A bit of experimentation later and I wound up with the code below which seems to work as desired (in case anyone else is looking for an answer to this).
I also changed the Send to Dispatch based on a comment in the DataGrid section of the User Guide.

The last bit was that if I selected more than one row in the selection grid, the edit didn't happen.
I surmised that this was because the next AddData closed the edit. That appears to be correct since moving the EditCellOfIndex out of the loop fixed that problem.

Turns out I wasn't all that far away. ;-) Thanks again for the assist.

The resulting code:

Code: Select all

on mouseUp
   lock screen
   put empty into lineNo
   put the dgHiLitedLines of group selectComponentGrid into rowNumbers
   put the dgData of group selectComponentGrid into rows
   repeat for each item rowNumber in rowNumbers
      put rows[rowNumber] into row 
      Dispatch "AddData" to group bomGrid of card inventoryItem of stack inventory with row
      if lineNo is empty then
         put the result into lineNo
      end if
   end repeat
   if lineNo is not empty then
      # Set focus to the first new row's quantity field.
      dispatch "EditCellOfIndex" \
            to group bomGrid on card "inventoryItem" of stack "inventory" \
            with "quantity", lineNo 
   end if
   unlock screen
   close this stack
end mouseUp

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”