How to add an empty line to 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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mluka
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 73
Joined: Sun Jun 17, 2007 12:08 am
Location: Montréal, Canada

How to add an empty line to a datagrid

Post by mluka » Sat Jun 23, 2018 5:50 pm

Hi all.

The LC lessons (about DataGrid "DG") seem to assume that you want to populate a DG with data that you already have either via the Contents property or via a handler (that sets the dgData with data you already have).

But I want to be able to input data directly into the datagrid, as a user, without accessing properties or handlers.

In the comments section of the "How Do I Open a Table Cell for Editing" lesson, I found a tantalizing hint by Trevor DeVore; he wrote: "Just add a new row to the data grid (empty values in all columns) and then open the cell of the new row for editing."

I was able to find a section in the Dictionary/Guide: "How Do I Add a Row of Data to an Existing Data Grid?".

So, before I launch myself in the wrong direction, can someone please confirm that the right approach would be something like this:
  1. Put "" into the appropriate elements of the array
  2. Figure out what the next line number is going to be (I want to add a row at the end)
  3. Use "dispatch "ABC" to group" cmd
  4. Put the insertion point inside the first element of the new (empty) row so the user can start entering data (with cmd EditFieldText)
In general terms, is that the right approach? The best approach (don't hesitate to suggest a better way)?

Thanks!
Michel
Montréal, Canada

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm
Location: McKenna, WA

Re: How to add an empty line to a datagrid

Post by quailcreek » Sun Jun 24, 2018 12:16 am

Take a look at DataGrid Helper. It's well worth the money. TheSlug has this covered.
Tom
MacBook Pro OS Mojave 10.14

Zryip TheSlug
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 163
Joined: Tue Jan 26, 2010 10:15 pm
Contact:

Re: How to add an empty line to a datagrid

Post by Zryip TheSlug » Mon Jun 25, 2018 7:33 pm

For adding an empty row at the end of the datagrid, you can use:

Code: Select all

send "AddData" to grp "datagrid 1"
Where "datagrid 1" is the name of your datagrid.

For editing the first cell of the new added line you need 2 things:
- the name of the first column
- the number of the new added row

For the name of the column you can use it directly, for example "column 1"
For the number of the new line you can use the following syntax:

Code: Select all

the dgNumberOfLines of grp "datagrid 1"
DataGrid Helper is installing something similar to the following script, with something more structured, though:

Code: Select all

on mouseUp
   local tMyDataGrid, tTheFirstCol, tTheCurrentLine
   
   put "datagrid 1" into tMyDataGrid
   
   send "AddData" to grp tMyDataGrid -- add the new line
   
   put line 1 of the dgProp["columns"] of grp tMyDataGrid into tTheFirstCol -- get the first column. You can also adapt the script to edit column 2, 3 or 4 etc
   put the dgNumberOfLines of grp tMyDataGrid into tTheCurrentLine -- get the number of the last line
   
   set the dgHilitedLines of grp tMyDataGrid to tTheCurrentLine -- select the new line (this is also scrolling the datagrid to display the fresh line)
   
   dispatch "EditCell" to grp "datagrid 1" with tTheFirstCol, tTheCurrentLine -- edit the first cell of the new line
end mouseUp
TheSlug
http://www.aslugontheroad.com - Tutorials, demo stacks and plugins for LiveCode
Data Grid Helper - An intuitive interface for building LiveCode's Data Grids
Excel Library- Extends the LiveCode language for controlling MS Excel

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: How to add an empty line to a datagrid

Post by marksmithhfx » Sun Aug 30, 2020 12:57 pm

Zryip, thanks for the code. There is some good information in there.

Best,
Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: How to add an empty line to a datagrid

Post by marksmithhfx » Mon Aug 31, 2020 8:25 am

Here's my solution to "add an empty line" since I've not seen it here:

Code: Select all

   put empty & tab & “somevalue” & tab & myVar & tab & empty & tab into theRowData -- new empty row of data
   put “Name” & cr & “Address” & cr & “Something else” & cr & "purgeDate" into theDataColumns
   put the dgNumberOfLines of me + 1 into theLine -- the new DG line number
   AddLine theRowData, theDataColumns, theLine -- this adds a new line to the DG
   ScrollLineIntoView theLine -- this scrolls the line into view
Now I have another problem. In dev, and when compiled to macOS, if this code is triggered by a return key by being on the last line of a DG it inserts a new blank row, scrolls to the row, and puts the cursor in the first field. Fantastic, wonderful exactly what a user (and I) would want. On iOS not so much. It creates the new blank row in the DG and scrolls to it but does not place the cursor in the first field, or any field. You have to specifically select it. Question: is there a way to nudge the cursor into the first field of this new blank row?

Cheers and thanks,
Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”