color in grid

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: color in grid

Post by andyglezl »

franco wrote: Sun Oct 08, 2017 9:18 am This works great, but I can not get it to work with a grid.
What I need is a table not an array in a grid.
Serges idea works good but I can not get it to work with a table not an array.
Thanx .. Franco

Hello Franco
just change ...

bColor := { || if ( This.CellRowIndex/2 == int(This.CellRowIndex/2) ,(BLUE), (PURPLE))}

by:

bColor4 := { || if ( unit_price < 1000, RED, WHITE ) }
fColor4 := { || if ( unit_price < 1000, YELLOW, BLACK ) }

DYNAMICBACKCOLOR { ,,, bColor4 } ;
DYNAMICFORECOLOR { ,,, fColor4 }

----------------------------------------------------------------------
You have to remove

ONCHANGE BC_Grid_1()
Andrés González López
Desde Guadalajara, Jalisco. México.
User avatar
serge_girard
Posts: 3161
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: color in grid

Post by serge_girard »

Franco, Indeed this works with an array. I never used a table directly so maybe Andres' example will fit for you?
Serge
There's nothing you can do that can't be done...
franco
Posts: 816
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: color in grid

Post by franco »

This works in 3.2 windows8 but can not get to work in 3.44 windows10

Code: Select all

#include "hmg.ch"

Function Main
	PRIVATE BCOLOR, FCOLOR, MCOLOR := '192,192,192'
	FILS()
	set century on
	 * bColor := { || if ( This.CellRowIndex/2 == int(This.CellRowIndex/2) , {100,100,190} , {192,192,192} ) }
      *fColor := { || if ( This.CellRowIndex/2 == int(This.CellRowIndex/2) ,blue  , RED ) }
	bColor := { || if ( unit_price < 1 .OR. '2' $ DESC, RED, )} //{192,192,192} )}  
	fColor := { || if ( unit_price < 1, BLACK,BLACK )}  //YELLOW, BLACK ) }
	
	
	*IF .NOT. ISWINDOWACTIVE(FORM_2)
		DEFINE WINDOW Form_2 ;
		AT 110, 10 ;			
		WIDTH 1250 ;				
		HEIGHT 700 ;		
		TITLE 'Sales Report Screen' ;			
		MAIN ;
		NOSIZE 	;     
		ON RELEASE {||{Closer2fils()}}  
		
		
			DEFINE TEXTBOX TEXT_1 	
			ROW 150
			COL 280
			WIDTH 200 
			HEIGHT 20 
			UPPERCASE .T.
			FIELD TEMP5->DESC 
			
		END TEXTBOX

		DEFINE GRID Grid_2
			ROW	200
			COL	30        //140
			WIDTH 1190         //1100
			HEIGHT 200    //370
			BACKCOLOR  { 180, 180, 200}
			FONTCOLOR  { 0, 0, 0 }	
			HEADERS {"Invoice", "Item" ,"Sellprice","Desc"}
			COLUMNCONTROLS { {'TEXTBOX','CHARACTER',"!!!!!!!!"},{'TEXTBOX','CHARACTER',"!!!!!!!!!!!!!!!"},;
							{'TEXTBOX','NUMERIC','9999999.99'},{'TEXTBOX','CHARACTER',"!!!!!!!!!!!!!!!!!!!!!!!!!!"}}
			WIDTHS {80, 95, 100 ,150}
			JUSTIFY {0,0,1,0}
			COLUMNWHEN { { || .T. },{ || .T. }, { || .T. }, { || .T. }}
			ROWSOURCE 'temp5'  
			COLUMNFIELDS { 'INV_NO' , 'ITEM_NO','UNIT_PRICE','DESC'}
			VALUE {1,1}
			ONCHANGE   BC_Grid_1()
			DYNAMICBACKCOLOR { BCOLOR,BCOLOR, BCOLOR,BCOLOR}
			DYNAMICFORECOLOR { FCOLOR,FCOLOR,FCOLOR,FCOLOR}
			ALLOWEDIT .T.
			CELLNAVIGATION .T.
			TABSTOP .F.
		END GRID
	
	END WINDOW
		
		Form_2.Activate
		
	RETURN //NIL
*********************************************************

	

	Function Fils
		Local CF1 := {}
		CF1 := {}
		aADD(CF1,{'INV_no' , 'C' , 8,0})
		AADD(CF1,{'ITEM_NO', 'C' , 15,0})
		Aadd(CF1,{'DESC',   'C', 26,0})
		Aadd(CF1,{'UNIT_PRICE', 'N' , 10,2})

		if ! hb_dbcreatetemp("temp5", cf1)
			msgbox("Cannot create temporary table: Item")
			RELEASE WINDOW ALL
			return nil
		endif
		if select("temp5") = 0
			use temp5 new
		endif
		select temp5 
		index on inv_no to temp5
		temp := 1
		do while temp < 51
			append blank
			replace inv_no with 'IN'+alltrim(str(temp+1000))
			replace item_no with 'IT'+alltrim(str(temp+100))
			replace unit_price with temp*100
			replace desc with 'DESC '+STR(TEMP)
			temp:=temp+1
			loop
		enddo
		GO 25 
		REPLACE UNIT_PRICE WITH 0
		return

	Function CloseR2fils

		close temp5
		release temp5
	
	return

	STATIC FUNCTION BC_Grid_1()
*****************************
LOCAL A := FORM_2.GRID_2.VALUE
FORM_2.GRID_2.SAVE
FORM_2.GRID_2.VALUE := A
RETURN 
Franco ;)
All The Best,
Franco
Canada
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: color in grid

Post by mol »

It's because GRID does not modify record pointer in temp5 database
Change your BC_Grid_1() function and observe results:

Code: Select all


STATIC FUNCTION BC_Grid_1()
*****************************
LOCAL A := FORM_2.GRID_2.VALUE
FORM_2.GRID_2.SAVE
FORM_2.GRID_2.VALUE := A
MsgInfo(temp5->Inv_No+ " Recno " + str(temp5->(RecNo())) + "   Grid RecNo " + str(FORM_2.GRID_2.RecNo))
RETURN 
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: color in grid

Post by mol »

It's unresolvable with GRID, I think.
The only way is to use BROWSE or VirtualGrid
User avatar
mol
Posts: 3718
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: color in grid

Post by mol »

I've modified your sample for VirtualGrid

Code: Select all

#include "hmg.ch"

Function Main
	PRIVATE BCOLOR, FCOLOR, MCOLOR := '192,192,192'
	
	// VirtualGrid  modification
	private aTemp5 := {}
		
	FILS()
	
	// VirtualGrid  modification
	GetTemp5Data()
	
	set century on
	 * bColor := { || if ( This.CellRowIndex/2 == int(This.CellRowIndex/2) , {100,100,190} , {192,192,192} ) }
      *fColor := { || if ( This.CellRowIndex/2 == int(This.CellRowIndex/2) ,blue  , RED ) }
	bColor := { || if ( unit_price < 1 /*.OR. '2' $ DESC*/, RED, {192,192,192} )}
	fColor := { || if ( unit_price < 1, BLACK, BLACK )}  //YELLOW, BLACK ) }
	
	
	*IF .NOT. ISWINDOWACTIVE(FORM_2)
		DEFINE WINDOW Form_2 ;
		AT 110, 10 ;			
		WIDTH 1250 ;				
		HEIGHT 700 ;		
		TITLE 'Sales Report Screen' ;			
		MAIN ;
		NOSIZE 	;     
		ON RELEASE {||{Closer2fils()}}  
		
		
			DEFINE TEXTBOX TEXT_1 	
			ROW 150
			COL 280
			WIDTH 200 
			HEIGHT 20 
			UPPERCASE .T.
			FIELD TEMP5->DESC 
			
		END TEXTBOX

		DEFINE GRID Grid_2
			ROW	200
			COL	30        //140
			WIDTH 1190         //1100
			HEIGHT 200    //370
			BACKCOLOR  { 180, 180, 200}
			FONTCOLOR  { 0, 0, 0 }	
			HEADERS {"Invoice", "Item" ,"Sellprice","Desc"}
			COLUMNCONTROLS { {'TEXTBOX','CHARACTER',"!!!!!!!!"},{'TEXTBOX','CHARACTER',"!!!!!!!!!!!!!!!"},;
							{'TEXTBOX','NUMERIC','9999999.99'},{'TEXTBOX','CHARACTER',"!!!!!!!!!!!!!!!!!!!!!!!!!!"}}
			WIDTHS {80, 95, 100 ,150}
			JUSTIFY {0,0,1,0}
			COLUMNWHEN { { || .T. },{ || .T. }, { || .T. }, { || .T. }}
			//ROWSOURCE 'temp5'  // VirtualGrid
			COLUMNFIELDS NIL	// VirtualGrid  modification { 'INV_NO' , 'ITEM_NO','UNIT_PRICE','DESC'}
			VALUE {1,1}
			ONCHANGE   BC_Grid_1()
			DYNAMICBACKCOLOR { {|| RowBackColor()}, {|| RowBackColor()}, {|| RowBackColor()}, {|| RowBackColor()} }
			DYNAMICFORECOLOR { {|| RowForeColor()}, {|| RowForeColor()}, {|| RowForeColor()}, {|| RowForeColor()} }
			VIRTUAL .T.	// VirtualGrid  modification
			ALLOWEDIT .f.	// VirtualGrid  modification - BUT IT'S TO DO
			ONQUERYDATA GRid_2_OnQueryData()
			CELLNAVIGATION .T.
			TABSTOP .F.
		END GRID
	
	
	END WINDOW
	
	Form_2.Grid_2.ItemCount := len(aTemp5)
	
	Form_2.Activate
		
	RETURN //NIL
*********************************************************

	

	Function Fils
		Local CF1 := {}
		CF1 := {}
		aADD(CF1,{'INV_no' , 'C' , 8,0})
		AADD(CF1,{'ITEM_NO', 'C' , 15,0})
		Aadd(CF1,{'DESC',   'C', 26,0})
		Aadd(CF1,{'UNIT_PRICE', 'N' , 10,2})

		if ! hb_dbcreatetemp("temp5", cf1)
			msgbox("Cannot create temporary table: Item")
			RELEASE WINDOW ALL
			return nil
		endif
		if select("temp5") = 0
			use temp5 new
		endif
		select temp5 
		index on inv_no to temp5
		temp := 1
		do while temp < 51
			append blank
			replace inv_no with 'IN'+alltrim(str(temp+1000))
			replace item_no with 'IT'+alltrim(str(temp+100))
			replace unit_price with temp*100
			replace desc with 'DESC '+STR(TEMP)
			temp:=temp+1
			loop
		enddo
		GO 25 
		REPLACE UNIT_PRICE WITH 0
		return

	Function CloseR2fils

		close temp5
		release temp5
	

return

STATIC FUNCTION BC_Grid_1()
*****************************
	LOCAL A := FORM_2.GRID_2.VALUE
	FORM_2.GRID_2.SAVE
	FORM_2.GRID_2.VALUE := A
RETURN 

*---------------------------
function RowBackColor
	local nRow
	
	#define UNIT_PRICE_COLUMN 3
	nRow := This.QueryRowIndex
	if nRow>0
		return if( aTemp5[nRow, UNIT_PRICE_COLUMN] < 1, RED, {192,192,192} )  //YELLOW, BLACK ) }
	endif
	
 return {192,192,192}
*---------------------------	
function RowForeColor
	local nRow
	
	#define UNIT_PRICE_COLUMN 3
	nRow := This.QueryRowIndex
	if nRow>0
		return if( aTemp5[nRow, UNIT_PRICE_COLUMN] < 1, BLACK, BLACK )  //YELLOW, BLACK ) }
	endif
 return BLACK
*---------------------------	
procedure GetTemp5Data
	temp5->(DBGoTop())
	do while !temp5->(eof())
		aAdd(aTemp5, { temp5->INV_NO , temp5->ITEM_NO,temp5->UNIT_PRICE,temp5->DESC } )
		temp5->(DBSkip())
	enddo
 return 

*---------------------------	
function GRid_2_OnQueryData
	local nRow, nCol
		
	nRow := This.QueryRowIndex
	nCol := This.QueryColIndex
	
	if nRow>0 .and. nCol >0
		if nRow <= len(aTemp5)
			This.QueryData := aTemp5[nRow, nCol]
		endif
	endif
	
return
*---------------
franco
Posts: 816
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: color in grid

Post by franco »

I would really like to thank everyone who put time into helping with this problem. It was one of those things I would not quit on, but
after a couple of days of trying I just went to a label that pops up when unit_price is under specs. This works ok, I really wanted to use a grid.
Thanks again to all .... Franco ;)
All The Best,
Franco
Canada
Post Reply