How to update a cell in a Data Grid table?

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
MichaelBluejay
Posts: 230
Joined: Thu Jul 01, 2010 11:50 am

How to update a cell in a Data Grid table?

Post by MichaelBluejay » Wed Oct 16, 2019 4:18 pm

Is there a way to update just a single cell in a Data Grid table, without updating a whole row? The documentation is typically silent on whether it's possible to update the contents of just a single cell (or maybe it is documented, just very well hidden.) The best I can figure is that I have to update a whole row at a time.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to update a cell in a Data Grid table?

Post by Klaus » Wed Oct 16, 2019 4:22 pm

The datagrid API does not support this, so you will need to update the complete row.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: How to update a cell in a Data Grid table?

Post by dunbarx » Wed Oct 16, 2019 9:05 pm

I think of this differently.

I always extract the entirety of the DG with the dgText, do stuff in "ordinary" LC. and re-install. Thus I rarely distinguish between tasks involving one or more "cells", one or more rows or columns, or the whole shebang.

This is just laziness on my part. But my comfort zone is in LC, not in DG stuff. There is little penalty that I can see...

Surely if one is modifying the structure of the DG, then you need to do DG stuff. But if it just modifying text, I do it my way.

Craig

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

Re: How to update a cell in a Data Grid table?

Post by Zryip TheSlug » Thu Oct 17, 2019 3:31 pm

Hi Michael,

You can update the content of a cell by using the SetDataOfIndex or SetDataOfLine commands of the datagrid API.

Usage:


1. By using index reference

Code: Select all

SetDataOfIndex pIndex, pKey, pValue
where pIndex if the index of the line, pKey is the name of the column and pValue is the value to update


2. By using line reference

Code: Select all

SetDataOfLine pLine, pKey, pValue
where pLine is the number of the row, pKey is the name of the column and pValue is the value to update

The usage context of SetDataOfIndex and SetDataOfLine is usually the datagrid group or row behavior, but you can use it from outside (a button outside the datagrid for example) by sending or dispatching the command to the datagrid group.


3. Example for updating the column "Product", row 3 with the new value "Product A" from a button outside the datagrid

Code: Select all

dispatch "SetDataOfLine" to grp "MyDataGrid" with 3, "Product", "Product A"

An important thing to know with the both commands SetDataOfIndex and SetDataOfLine is you have to refresh the row once updated to have the value appear in the cell.
For doing that you have two commands: RefreshIndex (use it in conjonction with SetDataOfIndex ) or RefreshLine (use it in conjonction with SetDataOfLine)

So if we keep our example, the final code to use is:

Code: Select all

dispatch "SetDataOfLine" to group "MyDataGrid" with 3, "Product", "Product A"
dispatch "RefreshLine" to group "MyDataGrid" with 3

Have a look to this lesson for more informations and a full example:
http://lessons.livecode.com/m/datagrid/ ... a-in-a-row

And to the datagrid API where SetDataOfLine, RefreshLine, SetDataOfIndex, RefreshIndex are described:
http://lessons.livecode.com/m/datagrid/ ... a-grid-api
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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to update a cell in a Data Grid table?

Post by Klaus » Thu Oct 17, 2019 3:50 pm

Oh, the datagrid never ceases to surprise me, did not know this! :shock:

Sorry for my false posting, Michael!

MichaelBluejay
Posts: 230
Joined: Thu Jul 01, 2010 11:50 am

Re: How to update a cell in a Data Grid table?

Post by MichaelBluejay » Fri Oct 25, 2019 10:49 pm

Thank you Zryip TheSlug, that's exactly what I needed.

Post Reply