LiveCode LessonsLiveCode Data Grid Working With Data Grids (Forms & Tables)How Do I Add A Row Of Data To An Existing Data Grid?

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 theDataA["FirstName"]

put "Last Name" into theDataA["LastName"]

put "Title" into theDataA["Title"]

put  1 into theLineNo

dispatch "AddData" to group "DataGrid" with theDataA, theLineNo

put the result into theNewIndex -- 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 theRowData

put "FirstName" & cr & "LastName" & cr & "Title" into theDataColumns

put the dgNumberOfLines of me + 1 into theLineNo

AddLine theRowData, theDataColumns, theLineNo

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 theNewIndex

or

ScrollLineIntoView theLineNo

2 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

Add your comment

E-Mail me when someone replies to this comment