Page 1 of 1

DataGrid: Setting bg color of cells conditionally

Posted: Sat Sep 29, 2012 11:21 pm
by sritcp
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.

Re: DataGrid: Setting bg color of cells conditionally

Posted: Tue Oct 02, 2012 2:17 pm
by Klaus
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

Re: DataGrid: Setting bg color of cells conditionally

Posted: Tue Oct 02, 2012 2:54 pm
by dunbarx
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

Re: DataGrid: Setting bg color of cells conditionally

Posted: Tue Oct 02, 2012 8:46 pm
by sritcp
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.

Re: DataGrid: Setting bg color of cells conditionally

Posted: Thu Jul 20, 2017 1:39 pm
by Simon Knight
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.

Re: DataGrid: Setting bg color of cells conditionally

Posted: Thu Jul 20, 2017 1:50 pm
by dunbarx
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

Re: DataGrid: Setting bg color of cells conditionally

Posted: Thu Jul 20, 2017 3:29 pm
by Simon Knight
Interesting, I did not know that it was possible to address the cells from outside.

Re: DataGrid: Setting bg color of cells conditionally

Posted: Sat Jul 22, 2017 1:49 am
by sritcp
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