How Do I Add A Row Of Data To An Existing Data Grid?

Sometimes you may want to add a row of data to a Data Grid without having to set the dgData or dgText property. You can add a single row of data by calling the AddData or AddLine commands of a Data Grid.

Using AddData

Using AddData

To use AddData you create an array containing the values for the new row. Here is an example of how to add a new row and have it appear as line 1 in a Data Grid. This example script resides outside of the Data Grid group script so AddData is not in the message path. This is why the dispatch command is used.

put "First Name" into tDataA["FirstName"]
put "Last Name" into tDataA["LastName"]
put "Title" into tDataA["Title"]
put the dgNumberOfLines of group "DataGrid" + 1 into tLineNo
dispatch "AddData" to group "DataGrid" with tDataA, tLineNo
put the result into tNewIndex -- integer if successful, error string otherwise

Using AddLine

Using AddLine

To use AddLine you create a tab delimited string of text containing the values for the new row. You also need to tell the Data Grid the names of the columns that the data should map to. Here is an example of how to add a new row and have it appear as the last line in a Data Grid. This example script resides inside the Data Grid group script so AddLine is in the message path.

put "First Name" & tab & "Last Name" & tab & "Title" into tRowData
put "FirstName" & cr & "LastName" & cr & "Title" into tDataColumns
put the dgNumberOfLines of me + 1 into tLineNo
AddLine tRowData, tDataColumns, tLineNo

Scrolling Data Into View And Getting The Data Control

After you add the data to the Data Grid you may want to scroll the new row into view. You can call ScrollIndexIntoView or ScrollLineIntoView to do this.

ScrollIndexIntoView tNewIndex

or

ScrollLineIntoView tLineNo

6 Comments

Bruce Peaslee

AddLine fails...

Type Handler: can't find handler
Object btnNew
Line AddLine tRowData, tColumns, tLineNo
Hint AddLine

Trevor DeVore

@Bruce: the error says that the handler can't be found so you are probably trying to execute the code from outside of the Data Grid group. If this is the case then you need to specifically target the Data Grid group using "dispatch" as in the first example:

dispatch "AddLine" to group "DataGrid" with theRowData, theDataColumns, theLineNo

Mark

Hi, does executing AddLine (AddLine theRowData, theDataColumns, theLine) from inside the DG trigger FillInData and LayoutControl messages? The problem I am having is that when I call AddLine in an established DG it adds the new row correctly. But on initiation of a new app it is not laying out the first line properly. In fact it looks like it does in the Row Template so I don't think any LayoutControl is happening. I am wondering what the correct sequence of calls should be if the first thing you do is Add-a-Line? (The first line added is a blank line used for data input. There are no lines in the DG before that). If it doesn't trigger them, can I call them myself (although I'm not sure using what parameters). Thanks

Elanor Buchanan

Hi Mark

Yes both FillInData and LayoutControl should be called after executing "AddLine". Is there a chance there is an error in one of the handlers so they do not finish executing? You should be able to add breakpoints in your row script to see what is happening.

Kind regards

Elanor

Mark

Thanks Elanor. Another question. ScrollLine/IndexIntoView scrolls the new line into view but doesn't actually select anything. Is there a way to move the insertion point/cursor into a field in the new record? Actually this behavior does happen by default (I know not how) on a new list, but then if you re-arrange rows all over the place it stops happening and the cursor jumps to some random location in the DG when a new line is added. What I need to do is explicitly direct the cursor to select the text/field of a specific DG row and column (the last one in this case? I've not been able to figure that out yet. Thanks for your help.

Elanor Buchanan

Hi Mark

I have done some testing and this seems to work but I probably haven't done as much rearranging etc so give it a try and let me know if it doesn't work.

You can use dgDataControlOfLine[theLineNo] to get the long id of the data control associated with a line, then you can focus on a field in that group. So something like

on AddNew
put "AA" & tab & "BB" into theRowData
put "FirstName" & cr & "LastName" into theDataColumns
put the dgNumberOfLines of me + 1 into theLineNo
AddLine theRowData, theDataColumns, theLineNo

ScrollLineIntoView theLineNo

put the dgDataControlOfLine[theLineNo] of me into tGroupID
select after field "FirstName" of tGroupID
end AddNew

I hope that helps.

Elanor

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.