Hiliting a cell in a datagrid [SOLVED]

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
bcmadore
Posts: 7
Joined: Tue May 24, 2011 5:48 pm
Contact:

Hiliting a cell in a datagrid [SOLVED]

Post by bcmadore » Wed May 08, 2013 9:42 pm

I have a Table style datagrid.

In my app I'm going through the datagrid and trying to update a database based on the contents of a cell in the table.

I'd like to hilte a cell in some way if the update fails. Something like make the text bold or set the background to red.

What would be the easiest way to accomplish this?
Last edited by bcmadore on Thu May 09, 2013 11:56 pm, edited 1 time in total.
Mac OS X 10.5.8 PPC | Mac OS X 10.6/7/8 Intel | LiveCode 5

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

Re: Hiliting a cell in a datagrid

Post by dunbarx » Thu May 09, 2013 1:51 pm

Hi.

I see nobody has touched this. I use datagrids, and still do not understand them.

The "cell" you mentioned is really a field, named according to its row and column position, as in field "Col 2 0002".

So you can always set field properties, either by using "the target" if you click on a cell, or by referencing the field directly:

set the textStyle of the target to "bold"
set the textStyle of fld "Col 2 0002" to "bold"

That sort of thing

Craig Newman

bcmadore
Posts: 7
Joined: Tue May 24, 2011 5:48 pm
Contact:

Re: Hiliting a cell in a datagrid

Post by bcmadore » Thu May 09, 2013 4:48 pm

Trying:

Code: Select all

put "Col 2" into theColumn
   put the dgHilitedLine of grp "dgTest" into theLineNo
   put theColumn & comma && theLineNo & cr after fld "debug"
   
   
   dispatch "EditCell" to grp "dgTest" with "theColumn, theLineNo" 
   set the textStyle of the target to "bold"
in a button sets the text style of the button pretty well.

Trying:

Code: Select all

   local fldName
   put theColumn && zeroFormat(theLineNo) into fldName
   set the textStyle of fld  fldName of grp "dgTest" to "bold"
(zero format pads the leading zeros on the number)
instead of set the textStyle of the target to "bold" does two strange things.

1) Sets the text style of multiple lines
2) Fails after a certain number of lines

I can almost touch a solution...
Mac OS X 10.5.8 PPC | Mac OS X 10.6/7/8 Intel | LiveCode 5

bcmadore
Posts: 7
Joined: Tue May 24, 2011 5:48 pm
Contact:

Re: Hiliting a cell in a datagrid

Post by bcmadore » Thu May 09, 2013 11:56 pm

Ok, I'm going to share this so it's easy for others.

Verified in 6

1) Create a new button. I'll call mine "My Default Column Behavior". I put mine in the mainstack, but the substack the rest of the DataGrid could work too, if you're going to go further in customizing your DataGrid

2) Add the following script.

Code: Select all

on FillInData pData
   set the text of the long ID of me to pData ## temp workaround for
   
   local  tIndex, tDataOfLine, tTextStyle, tCol
   try
      put the dgIndex of me into tIndex
      put word 1 to 2 of the short name of me into tCol
      put getStyle(tIndex, tCol) into tTextStyle
      set the textStyle of field 1 of me to tTextStyle
      --if tTextStyle is not empty then answer tTextStyle
   end try
end FillInData

on LayoutControl pControlRect
end LayoutControl

on ResetData
   set the text me to empty
   
   local  tIndex, tDataOfLine, tTextStyle, tCol
   try
      put the dgIndex of me into tIndex
      put word 1 to 2 of the short name of me into tCol
      xStyle tIndex, tCol
      set the textStyle of field 1 of me to empty
   end try
end ResetData

on PreFillInData
   local  tIndex, tDataOfLine, tTextStyle, tCol
   try
      put the dgIndex of me into tIndex
      put word 1 to 2 of the short name of me into tCol
      xStyle tIndex, tCol
      set the textStyle of field 1 of me to empty
   end try
end PreFillInData

setprop dgHilite pBoolean
    if pBoolean then
        set the foregroundColor of me to the dgProp["hilited text color"] of the dgControl of me
    else
        set the foregroundColor of me to empty
    end if
end dgHilite

getprop dgDataControl
    return the long ID of me
end dgDataControl

command EditValue
    EditFieldText the long ID of me, the dgIndex of me, the dgColumn of me
end EditValue

on mouseDoubleUp pMouseBtnNum
    if pMouseBtnNum is 1 then
        if the dgProps["allow editing"] of the dgControl of me \
                and the dgColumnIsEditable[the dgColumn of me] of the dgControl of me then
            EditCellOfIndex the dgColumn of me, the dgIndex of me
            exit mouseDoubleUp
        end if
    end if
    pass mouseDoubleUp
end mouseDoubleUp
The base code for this can be had by using set the script of button "My Default Column Behavior" to the script of button "Default Column" of stack "revDataGridLibrary". See This Lesson on custom behaviors. You can use the Message Box for this if you wish.

3)Put this code in the script for your DataGrid

Code: Select all

local StyleGrid
function getStyle ind col
   return StyleGrid[ind][col]
end getStyle 

on setStyle ind col sty
   put sty into StyleGrid[ind][col]
   RefreshIndex ind
end setStyle

on xStyle ind col
   put empty into StyleGrid[ind][col]
end xStyle

on clearStyle
   put empty into StyleGrid
   RefreshList
end clearStyle
4) Point your DataGrid to your new handler. Enter set the dgProps["default column behavior"] of group "DataGrid 1" to the long id of button "My Default Column Behavior". You can use the Message Box for this if you wish.

5) Test it. I created a button with this script:

Code: Select all

on mouseUp
   local tIndex, tColumn, tStyle
   put "Col 1" into tColumn
   put "bold" into tStyle
   
      put the dgIndexOfLine[the dgHilitedLines of group "DataGrid 1" ] of group "DataGrid 1" into tIndex
   
      dispatch "SetStyle" to group "DataGrid 1" with tIndex, tColumn, tStyle
end mouseUp
You can handle other behaviors this way -- background color is a good one. You just set up a bunch of variables to store the data.

It lets me change the formatting on the fly, and it keeps the formatting info out of the DataGrid. You can "Send" or "Dispatch" from any script to alter the presentation this way. You also do not have to worry about getting your formatting in your dgText.

It was also nice to find a way to not have to create a script for each column.
Mac OS X 10.5.8 PPC | Mac OS X 10.6/7/8 Intel | LiveCode 5

Post Reply