Page 1 of 1

How to add an empty line to a datagrid

Posted: Sat Jun 23, 2018 5:50 pm
by mluka
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!

Re: How to add an empty line to a datagrid

Posted: Sun Jun 24, 2018 12:16 am
by quailcreek
Take a look at DataGrid Helper. It's well worth the money. TheSlug has this covered.

Re: How to add an empty line to a datagrid

Posted: Mon Jun 25, 2018 7:33 pm
by Zryip TheSlug
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

Re: How to add an empty line to a datagrid

Posted: Sun Aug 30, 2020 12:57 pm
by marksmithhfx
Zryip, thanks for the code. There is some good information in there.

Best,
Mark

Re: How to add an empty line to a datagrid

Posted: Mon Aug 31, 2020 8:25 am
by marksmithhfx
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