Change font color to grid cell runtime

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Change font color to grid cell runtime

Post by jairpinho »

Hi all, I'm trying to change the color of a value in a grid at runtime or when it loads the grid with add item for example

Code: Select all

               For i:= 1 To oQuery:LastRec()
                        oRow := oQuery:GetRow(i)
                                                                                              
  
                                                                                              
                                                       iif( oRow:fieldGet(12) > 50 ,SetProperty( "Form_Relatorio","Grid_1","FORECOLOR", 6, RED ) , { 0,0,0 } ) 
                                                                                              *----- Adiciona Registros no Grid
                                                                                              ADD ITEM {  Alltrim(STR(oRow:fieldGet(1)) )  ,;
                                                                                              DTOC( oRow:fieldGet(3) ) ,;
                                                                                              Alltrim(oRow:fieldGet(10) ),;
                                                                                              cMaquina ,;
                                                                                              Alltrim(oRow:fieldGet(6) ),;
                                                                                              Alltrim(STR(oRow:fieldGet(12) )),;
                                                                                              Alltrim(STR(oRow:fieldGet(14) )),;
                                                                                              Alltrim(STR(oRow:fieldGet(16) )),;
                                                                                              Alltrim(STR(oRow:fieldGet(18) )),;
                                                                                              Alltrim(STR(oRow:fieldGet(20) )),;
                                                                                              Alltrim(STR(oRow:fieldGet(22) )),;
                                                                                              Alltrim(STR(oRow:fieldGet(25) )),;
                                                                                              Alltrim(STR(oRow:fieldGet(24) )),;
                                                                                              Alltrim(oRow:fieldGet(11) )} TO Grid_1 OF Form_Relatorio
                                                                                              oQuery:Skip(1)
           
                                                                                              
 
               Next
               
 
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: Change font color to grid cell runtime

Post by andyglezl »

Tal vez estos Ejemplos te ayuden...
------------------------------------------
Maybe these Examples will help you ...


\HMG\3.4.3\SAMPLES\Controls\Grid\GRID_11 , GRID_12, GRID_13, GRID_24, GRID_26
Andrés González López
Desde Guadalajara, Jalisco. México.
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Change font color to grid cell runtime

Post by jairpinho »

ja testei todos desta forma so muda a cor na inicialização da grid com o form, preciso que mude a cor apos a grid estar criada com os valores ja carregados e quando alterados trocar de cor
---------------------------------------------------------
I tried all this way so it changes the color at the start of the grid with the form, I need to change the color after the grid is created with the values already loaded and when changed color change
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: Change font color to grid cell runtime

Post by andyglezl »

Maybe this...

Private bColor := { || if ( oRow:fieldGet(12) > 50 , RED , { 0,0,0 } ) }

or

Private bColor := { || if ( This.CellValue > 50 , RED , { 0,0,0 } ) }


When you define the grid...

DYNAMICFORECOLOR { bColor , bColor , bColor , nFields... }
Andrés González López
Desde Guadalajara, Jalisco. México.
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Change font color to grid cell runtime

Post by serge_girard »

Jair,

Use this construction:

Code: Select all

define grid .... with 6 fields
 DYNAMICBACKCOLOR  { { || BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()} ,{BC_Grid_1()} ,{BC_Grid_1()} }  // for every field in the grid !!
end grid
...

STATIC FUNCTION BC_Grid_1()
/*****************************/
LOCAL a
LOCAL aColors  := {{ HONEYDEW, HONEYDEW, HONEYDEW, HONEYDEW ,HONEYDEW, HONEYDEW } }  // EQUAL TO NUMBER OF COLUMNS == 6
LOCAL aItem    := Form_1.Grid_1.Item ( This.CellRowIndex )


DO CASE 
CASE EMPTY(aItem [4] ) // some condition to set color
   FOR a = 1 TO 6
      aColors [1] [a] := RED  
   NEXT a
OTHERWISE
   RETURN MISTYROSE
ENDCASE

RETURN aColors [1] [ This.CellColIndex ]

Then fill up grid !
Hope this helps !

Serge
There's nothing you can do that can't be done...
User avatar
jairpinho
Posts: 420
Joined: Mon Jul 18, 2011 5:36 pm
Location: Rio Grande do Sul - Brasil
Contact:

Re: Change font color to grid cell runtime

Post by jairpinho »

serge_girard wrote: Thu Jun 01, 2017 7:26 am Jair,

Use this construction:

Code: Select all

define grid .... with 6 fields
 DYNAMICBACKCOLOR  { { || BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()} ,{BC_Grid_1()} ,{BC_Grid_1()} }  // for every field in the grid !!
end grid
...

STATIC FUNCTION BC_Grid_1()
/*****************************/
LOCAL a
LOCAL aColors  := {{ HONEYDEW, HONEYDEW, HONEYDEW, HONEYDEW ,HONEYDEW, HONEYDEW } }  // EQUAL TO NUMBER OF COLUMNS == 6
LOCAL aItem    := Form_1.Grid_1.Item ( This.CellRowIndex )


DO CASE 
CASE EMPTY(aItem [4] ) // some condition to set color
   FOR a = 1 TO 6
      aColors [1] [a] := RED  
   NEXT a
OTHERWISE
   RETURN MISTYROSE
ENDCASE

RETURN aColors [1] [ This.CellColIndex ]

Then fill up grid !
Hope this helps !

Serge

Code: Select all


*************************************************************************************************************************************
Function Relatorio_Esterilizacao()
*************************************************************************************************************************************
Private aColunaX := {}
Private aColunaY := {}
Private aColunaXX := {}
Private aColor := {}
Private x2 := 0   
Private y2 := 0 
//Private fColor := {}


	
IF !IsWindowDefined(Form_Relatorio)	
        Load Window Form_Relatorio
			Form_Relatorio.Row := 10
			Form_Relatorio.Col := 10
			Pesquisa_Ciclos_Maquinas()			
        Form_Relatorio.Activate
ENDIF
return



STATIC FUNCTION BC_Grid_1()
/*****************************/
LOCAL a
LOCAL aColors  := {{ BLACK, BLACK, BLACK, BLACK ,BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK, BLACK } }  // EQUAL TO NUMBER OF COLUMNS == 6
LOCAL aItem    := {}


//{60,80,70,130,100,60,60,60,60,60,60,70,70,250}
//{{ || BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()}, { BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()} ,{BC_Grid_1()} ,{BC_Grid_1()} ,{BC_Grid_1()},{BC_Grid_1()},{BC_Grid_1()} }
IF IsWindowActive(Form_Relatorio)

aItem    := Form_Relatorio.Grid_1.Item (This.CellRowIndex)



  FOR a = 1 TO 14
		if Val(aItem [6]) > 50 // some condition to set color//  for val to colunn?
			aColors [1] [a] := RED  
		endif
   NEXT a

endif

 RETURN aColors[1][ This.CellColIndex ] // line 55

Attachments
erro.png
erro.png (28.47 KiB) Viewed 5263 times
Jair Pinho
HMG ALTA REVOLUÇÃO xBASE
HMG xBASE REVOLUTION HIGH
http://www.hmgforum.com.br
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Change font color to grid cell runtime

Post by serge_girard »

Jair , pls also FMG!

Serge
There's nothing you can do that can't be done...
Marin
Posts: 33
Joined: Tue Dec 20, 2016 1:39 pm
DBs Used: DBF
Location: Bulgaria, Sofia
Contact:

Re: Change font color to grid cell runtime

Post by Marin »

Hi, jairpinho,

Serge gave you a good and working solution.

May I propose you my approach - you might find it useful.

Kind regards,
Marin

Code: Select all

// 1st step: Define colors and the matching condition for changing colors
PRIVATE GRDForeColor := { || IF ( AtCellColFn1( HMG_LEN(aCOLUMNFIELDS) ) == 1, { 255, 0, 0 }, { 0, 0, 0 } ) } // {255,0,0}=light red ; {0,0,0}=black
PRIVATE arr_GRDForeColor[HMG_LEN(aCOLUMNFIELDS)] // An array with the FOREGROUND color for each column; The number of columns is equal to the number of fields as defined by aCOLUMNFIELDS[21]
FOR i:=1 TO HMG_LEN(arr_GRDForeColor)
	arr_GRDForeColor[i] := GRDForeColor // This codeblock-variable is assigned to each element from the array
NEXT

...........................
// 2nd step: GRID-control definition
    // GRID-control definition (the control belongs to window ChildWin_5; the control is data-bound)
    DEFINE GRID Grid_5
        TOOLTIP ''
        ROW 180
        COL 5
        WIDTH (ChildWin_5.WIDTH - 30)
        HEIGHT (ChildWin_5.HEIGHT - 260)
        FONTNAME 'Courier New'
        FONTSIZE 11
        ROWSOURCE 'KABOSN'//* dbf_file
        COLUMNFIELDS aCOLUMNFIELDS
        HEADERS aHEADERS
        WIDTHS aWIDTHS
        COLUMNCONTROLS aCOLUMNCONTROLS
        COLUMNWHEN aCOLUMNWHEN
        COLUMNVALID aCOLUMNVALID
        ALLOWEDIT .T.
        ALLOWAPPEND .F.
        ALLOWDELETE .T.
        CELLNAVIGATION .T.
	VALUE { 1, 1 }
        EDITOPTION GRID_EDIT_DEFAULT
        DYNAMICBACKCOLOR arr_GRDBackColor
	DYNAMICFORECOLOR arr_GRDForeColor // Change the color to red if the record has been deleted, i.e. when '1' is present in the rightmost GRID column
	ONCHANGE Grid_5OnChangeFn1()
    END GRID
    
...........................
// 3rd step: Function to produce the result, which is being checked in the 1st step

***************************************************************************************************
Function AtCellColFn1(nColIndex)
***************************************************************************************************
// Returns the value of the cell in the rightmost GRID column => if == '1' then the GRID changes the foreground color to RED 
Local nRowIndex := This.CellRowIndex
Local nRet := Val(GetProperty( "ChildWin_5", "Grid_5", "CellEx", nRowIndex, nColIndex ))

Return nRet
    
User avatar
serge_girard
Posts: 3165
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Change font color to grid cell runtime

Post by serge_girard »

Hi Marin,

Thanks for nice example!


Serge
There's nothing you can do that can't be done...
Marin
Posts: 33
Joined: Tue Dec 20, 2016 1:39 pm
DBs Used: DBF
Location: Bulgaria, Sofia
Contact:

Re: Change font color to grid cell runtime

Post by Marin »

serge_girard wrote: Fri Jun 02, 2017 10:00 am Hi Marin,

Thanks for nice example!


Serge
I learned a lot from you, Serge!
Thank you.

Marin
Post Reply