DataGrid: Setting bg color of cells conditionally

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
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

DataGrid: Setting bg color of cells conditionally

Post by sritcp » Sat Sep 29, 2012 11:21 pm

Hi:

I am trying to set the cell color to green if the cell entry is 1, red if it is 0. I added a switch statement to the FillinData handler of the behavior script, as follows:

Code: Select all

on FillInData pDataArray
   set the text of field "Label" of me to pDataArray["label 1"]

   switch the text of field "Label" of me
      case 1
         set the backGroundColor of me to green
      case 0
         set the backGroundColor of me to red
   end switch
  
end FillInData
Well, nothing happened! Any help is welcome.

Thanks,
Sri.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: DataGrid: Setting bg color of cells conditionally

Post by Klaus » Tue Oct 02, 2012 2:17 pm

Hi Sri,

well, ME in your script refers to the datagrid itself and there is nothing that could HEIR this backgroundcolor obviously!
Maybe because the objects have their own bg color set?

But you actually want to colorize a certain FIELD, right?
In that case try this:

Code: Select all

...
 switch the text of field "Label" of me
      case 1
         set the backGroundColor of fld "Label" of me to green
      case 0
         set the backGroundColor of fld "Label" of me to red
   end switch
...
Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: DataGrid: Setting bg color of cells conditionally

Post by dunbarx » Tue Oct 02, 2012 2:54 pm

Klaus.

I tried several things like this to try to help when the post first appeared. I got no errors, but also no results:

set the backgroundColor of fld "col 3 0002" of grp "dg3" to "red"

This DG group I used is named "dg3", and typically, the fields are named in this row/column format.

Nada.

Craig

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: DataGrid: Setting bg color of cells conditionally

Post by sritcp » Tue Oct 02, 2012 8:46 pm

I have finally solved it!

1. It has to be set in the fillinData of the custom column behavior script, not the dataGrid behavior script
2. The opacity must be set to "true" (default is "false", so the background bands of alternating row colors could show)

Here's the script:

Code: Select all

on FillInData pData
   set the text of field 1 of me to pData
   
   if the text of field 1 of me is 1 then
      set the opaque of me to true
      set the backgroundColor of me to green 
   else 
      set the opaque of me to true
      set the backgroundColor of me to red 
   end if
end FillInData
See the result below.

Thanks,
Sri.
Attachments
datagrid1.jpg
datagrid1.jpg (26.32 KiB) Viewed 6499 times

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: DataGrid: Setting bg color of cells conditionally

Post by Simon Knight » Thu Jul 20, 2017 1:39 pm

Hi, I know that this is a very old post but it is probably worth adding that the example above uses a "custom column behavior script" which works with a single column. If you want the colours to be applied across all the columns then the same code should be used in a behavior button that is set as the "default column behavior". This is set using commands like:

Code: Select all

Set the dgProps["default column behavior"] of group "DgLCSS" to the SelObj()
(see page 126 of the datagrid guide).

It is also worth noting that it is possible to set the colour of the text in FillInData but that it gets reset when "setprop dgHilite pBoolean" is called. Despite what is written in the comments :

Code: Select all

setprop dgHilite pBoolean
  -- This custom property is set when the highlight of your column template has
  -- changed. You only add script here if you want to customize the highlight.
  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
This is called after every row has been populated which means that the colour you set is very short lived.
best wishes
Skids

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: DataGrid: Setting bg color of cells conditionally

Post by dunbarx » Thu Jul 20, 2017 1:50 pm

Hi.

I looked over this thread, and especially my own post. I realized instantly that one must:

Code: Select all

on mouseUp
   set the opaque of fld "col 3 0002" of grp 1 to "true" --have to do this as well
   set the backgroundColor of fld "col 3 0002" of grp 1 to "red"
end mouseUp
Each field in a new DG is named in row/column format. Beyond that, they are all just LC field objects, and can be managed by hand.

Craig

Simon Knight
Posts: 845
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: DataGrid: Setting bg color of cells conditionally

Post by Simon Knight » Thu Jul 20, 2017 3:29 pm

Interesting, I did not know that it was possible to address the cells from outside.
best wishes
Skids

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: DataGrid: Setting bg color of cells conditionally

Post by sritcp » Sat Jul 22, 2017 1:49 am

Simon Knight wrote:It is also worth noting that it is possible to set the colour of the text in FillInData but that it gets reset when "setprop dgHilite pBoolean" is called. Despite what is written in the comments :

Code: Select all

setprop dgHilite pBoolean
  -- This custom property is set when the highlight of your column template has
  -- changed. You only add script here if you want to customize the highlight.
  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
This is called after every row has been populated which means that the colour you set is very short lived.
Off the top of my head....
You could save the text color as a custom property in the FillinData handler

Code: Select all

 ....
set the savedTextColor of me to tTextColor
....
and edit the setProp handler to reinstate the saved color

Code: Select all

....
--  set the foregroundColor of me to empty # is replaced by
  set the foregroundColor of me to the savedTextColor of me
....
Regards,
Sri

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”