Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamaic color setting in grid

Status
Not open for further replies.

kanagha

Programmer
Dec 19, 2002
8
IN
Hi,
The column's text box value in grid is getting calculated on button click event, and depending on that value, back color of text box in column should be differnet. When I tried with dynamicbackcolor property, its changing entire column color. Please tell me how I can chage back/fore color of cell depending on the cell value, which is getting calculated at run time.
Its very urgent. Please reply me soon

Thanks
 
kanagha

Take a look at Faq184-1939, it describes how to use the dynamicCurrentControl and change the color of the textbox under certain conditions. Copy the code into a program and run it to see how it works.

You may also look at FAQ184-1264. Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Thanks !
But I don't want the code on init event
.column2.DYNAMICCURRENTCONTROL = "dynamic()"
as I want to change the color of cell on the basis of its value and that value is getting calculated on the same button click.
I have written the ".column2.DYNAMICCURRENTCONTROL = "dynamic()" on the same button click. but its in for loop.

Please tell me where should i write it..in loop or not?
and event also?

I tried with the same code, using cursor ..but its giving me the cells of color depending on the last value in loop.

If I am not puting it in loop, then its giving error as "not a valid experssion for dynamiccurrentcontrol"
 
Thanks !
But I don't want the code on init event
.column2.DYNAMICCURRENTCONTROL = "dynamic()"
as I want to change the color of cell on the basis of its value and that value is getting calculated on the same button click.
I have written the ".column2.DYNAMICCURRENTCONTROL = "dynamic()" on the same button click. but its in for loop.

Please tell me where should i write it..in loop or not?
and event also?

I tried with the same code, using cursor ..but its giving me the cells of color depending on the last value in loop.

If I am not puting it in loop, then its giving error as "not a valid experssion for dynamiccurrentcontrol"

 
But I don't want the code on init event
.column2.DYNAMICCURRENTCONTROL = "dynamic()"
as I want to change the color of cell on the basis of its value and that value is getting calculated on the same button click.
I have written the ".column2.DYNAMICCURRENTCONTROL = "dynamic()" on the same button click. but its in for loop.


The Init event of the grid goes through each current records of the record source of the grid. But if you make a change to the record, just recall the init event again.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
This is the code I am writing on button click

Top1 =0
select _temp
CREATE CURSOR mycursor (sensor N(5),COLOR C(10))

FOR m.item = 1 TO reccount() step 1
TOP1 =TOP1+1
select _temp
go TOP1
m_heater = FbestHeater()
select _temp
GO TOP1
sens = ceil(Fmax_amp1(trim(m_heater),TOP1)/16)
&& this is the dymanic value of the column

SELECT mycursor
append blank
replace mycursor.sensor with sens
IF sens >2
replace mycursor.color with "RED" ELSE
replace mycursor.color with "BLUE"
ENDIF
Thisform.pageframe1.page3.grid1.column14.Text1.value= sens
ENDFOR

butclick=.T.
ThisForm.pageframe1.page3.Grid1.init
&& Recall to init event of grid as per your suggestion


This is the code I am writing in grid Init event
IF butclick=.T.
ThisForm.pageframe1.page3.Grid1.column14.DYNAMICCURRENTCONTROL = "dynamic()"
ENDIF

Following code is wriiten in one .prg file
FUNCTION DYNAMIC

DO CASE
CASE trim(myCursor.COLOR) = "RED"
RETURN "textRed1"
CASE trim(myCursor.COLOR) = "BLUE"
RETURN "textBlue1"
ENDCASE
ENDFUNC


DEFINE CLASS TextRed AS TEXTBOX
BACKCOLOR = RGB(255,0,0)
VISIBLE = .T.
ENDDEFINE
DEFINE CLASS TextBlue AS TEXTBOX
BACKCOLOR = RGB(0,128,255)
VISIBLE = .T.
ENDDEFINE

I tried as per your suggestion, ie by calling the init event of grid on button click.
But still all cells in column14 shows backcolor depending on last value in the cell.
 
Hi Kanagha,

Your code...

Top1 =0
select _temp
CREATE CURSOR mycursor (sensor N(5),COLOR C(10))

FOR m.item = 1 TO reccount() step 1
TOP1 =TOP1+1
select _temp
go TOP1
m_heater = FbestHeater()
select _temp
GO TOP1
sens = ceil(Fmax_amp1(trim(m_heater),TOP1)/16)
&& this is the dymanic value of the column

SELECT mycursor
append blank
replace mycursor.sensor with sens
IF sens >2
replace mycursor.color with "RED"
ELSE
replace mycursor.color with "BLUE"
ENDIF
Thisform.pageframe1.page3.grid1.column14.Text1.value= ;
sens
ENDFOR
butclick=.T.

ThisForm.pageframe1.page3.Grid1.column14.DynamicBackColor=;
"IIF(UPPER(ALLTRIM(myCursor.color)) = ;
"RED",RGB(255,0,0),RGB(0,0,128))"
ThisForm.PageFrame.Page3.Grid1.Refresh()


This will do the trick for you. :)
ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
Merry Christmas & Happy New Year [bigears] [party] [2thumbsup]

 
Thanks again!
ThisForm.pageframe1.page3.Grid1.column14.DynamicBackColor=;
"IIF(UPPER(ALLTRIM(myCursor.color)) = ;
"RED",RGB(255,0,0),RGB(0,0,128))"
I already tried this code.Its not working.
The dynamicbackcolor property of column works if we are taking recno() in the condition. IF i want to make even and odd color rows in different color, it works because we need to use recno in that, but if we will put any other condition in that, its showing color of the entire column depends on the last record.


 
Hej !
The piece of my application in the form.init:

ThisForm.Grid1.RecordSource='POM'
ThisForm.Grid1.Column1.ControlSource='POM.BUD'
ThisForm.Grid1.Column2.ControlSource='POM.OBW'
ThisForm.Grid1.Column2.DynamicForeColor="IIF('1'$pom.obws,RGB(255,0,0),RGB(0,0,0))"
ThisForm.Grid1.Column2.DynamicFontBold="IIF('1'$pom.obws,.T.,.F.)"

It's realy work. I think that is the same for DynamicBackColor. In the another event use method grid.refresh

Merry Christmas and Happy New Year for all guru's and 'grey' members (like i) of TEK-TIPS (especially for this forum)
Kind regards from Warsaw !!!!!
Monika (monikai@yahoo.com)
 
That's what I am saying.
Dynamicbackcolor works if we are accessing database.
But it doesn't work appropriate if we are giving any other condition.

 
kanagha

My suggestion was using dynamiccurrentcrontol and the other suggestions are using dynamicbackcolor. In order not to confuse the issue, I'll let you work through the other members' suggestions. Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Hello mgagnon,
Thanks for your help.
and I am not getting confused anyway
I am trying for this since last three days
I tried with dynamiccurrentcrontol also.
but the problem is that where should I call the "dynamic" function and where should I create the objects of textred and textblue.I want to know the event where should I write?
As your code was complete new creation.. i mean it was not on existing form, Still i tried to work similar to your code, I have given complete code what I am using. please go through it and let me know the solution.
One more thing.
Should I make that column's sparse property as false or not?
 
Hi Kanagha

Copy the code below and try the example..
***********************************************
** Save following code as Test.prg
** in command prompt .. DO test
** Read code in the Grids Init event
**********************************************
CREATE CURSOR test (cName C(20), email c(20))

INSERT INTO test (cName, email) VALUES ("experts","experts@Tek_tips.moc")
INSERT INTO test (cName, email) VALUES ("Dummy","dummy@dummy.moc")
INSERT INTO test (cName, email) VALUES ("ClayHead","clay@dummy.moc")
INSERT INTO test (cName, email) VALUES ("WoodBrain","wood@Tek_pits.moc")
INSERT INTO test (cName, email) VALUES ("IronHead","iron@dummy.moc")
INSERT INTO test (cName, email) VALUES ("BigHead","big@dummy.moc")
INSERT INTO test (cName, email) VALUES ("ShowMan","showbiz@dummy.moc")
INSERT INTO test (cName, email) VALUES ("NutHead","nuts@dummy.moc")
INSERT INTO test (cName, email) VALUES ("Ramani","ramani@Tek_tips.moc")
INSERT INTO test (cName, email) VALUES ("Mike","mike@Tek_tips.moc")
INSERT INTO test (cName, email) VALUES ("Rick","Rick@Tek_tips.moc")
INSERT INTO test (cName, email) VALUES ("Chris","chris@Tek_tips.moc")
INSERT INTO test (cName, email) VALUES ("Dsumzz","dsumzz@Tek_tips.moc")
INSERT INTO test (cName, email) VALUES ("mySelf","me@Tek_tips.moc")
LOCATE

myFields = "cName,email"
DO gsFindForm WITH myFields
=MESSAGEBOX("You have selected :"+ALLTRIM(cName)+" : "+email, 0+16, "Eureka!")

RETURN
**************************************************
PROCEDURE gsFindForm
PARAMETERS tcFields

IF PARAMETERS() < 1
RETURN .f.
ENDIF

PUBLIC oform1

oform1=NEWOBJECT(&quot;gsFindForm&quot;,'','',tcFields)
oform1.Show

RELEASE oForm1
RETURN
**************************************************
*-- Form: gsFindForm
*-- ParentClass: form
*-- BaseClass: form
*
DEFINE CLASS gsFindForm AS form

AutoCenter = .t.
Height = 242
Width = 420
DoCreate = .T.
Caption = &quot;FindForm&quot;
Name = &quot;gsFindForm&quot;
WindowType = 1
inrecno = 0
cFilter = &quot;&quot;
cFilterOld = &quot;&quot;

ADD OBJECT text1 AS textbox WITH ;
Height = 23, ;
Left = 84, ;
Top = 192, ;
Width = 336, ;
Name = &quot;Text1&quot;

ADD OBJECT cmdfirst AS commandbutton WITH ;
Top = 216, ;
Left = 0, ;
Height = 27, ;
Width = 84, ;
Caption = &quot;\<First&quot;, ;
Name = &quot;cmdFirst&quot;

ADD OBJECT cmdprevious AS commandbutton WITH ;
Top = 216, ;
Left = 84, ;
Height = 27, ;
Width = 84, ;
Caption = &quot;\<Previous&quot;, ;
Name = &quot;cmdPrevious&quot;

ADD OBJECT cmdnext AS commandbutton WITH ;
Top = 216, ;
Left = 168, ;
Height = 27, ;
Width = 84, ;
Caption = &quot;\<Next&quot;, ;
Name = &quot;cmdNext&quot;

ADD OBJECT cmdlast AS commandbutton WITH ;
Top = 216, ;
Left = 252, ;
Height = 27, ;
Width = 84, ;
Caption = &quot;\<Last&quot;, ;
Name = &quot;cmdLast&quot;

ADD OBJECT cmdexit AS commandbutton WITH ;
Top = 216, ;
Left = 336, ;
Height = 27, ;
Width = 84, ;
Cancel = .T., ;
Caption = &quot;E\<xit&quot;, ;
Name = &quot;cmdExit&quot;

ADD OBJECT grid1 AS grid WITH ;
Height = 216, ;
Left = 0, ;
Top = 0, ;
Width = 420, ;
Name = &quot;Grid1&quot;
DeleteMark = .F.
HighlightRow = .T.
ReadOnly = .T.
ForeColor = RGB(0,0,0)
BackColor = RGB(255,255,192)
GridLineColor = RGB(0,0,128)

PROCEDURE Init
PARAMETERS tcFields
IF PARAMETERS() < 1
RETURN .f.
ENDIF
LOCAL nCount
nCount = ALINES(laFields,tcFields,&quot;,&quot;)
WITH ThisForm.Grid1
.ColumnCount = nCount
FOR I=1 TO nCount
.Columns(I).ControlSource = laFields(i)
ENDFOR
.SetFocus()
ENDWITH
ENDPROC

PROCEDURE cmdfirst.Click
GO TOP
ThisForm.Grid1.SetFocus()
ThisForm.Refresh()
ENDPROC

PROCEDURE cmdprevious.Click
IF NOT BOF()
SKIP -1
ENDIF
ThisForm.Grid1.SetFocus()
ThisForm.Refresh()
ENDPROC

PROCEDURE cmdnext.Click
IF NOT EOF()
SKIP
ENDIF
ThisForm.Grid1.SetFocus()
ThisForm.Refresh()
ENDPROC

PROCEDURE cmdlast.Click
GO BOTTOM
ThisForm.Grid1.SetFocus()
ThisForm.Refresh()
ENDPROC

PROCEDURE cmdexit.Click
ThisForm.Release()
ENDPROC

PROCEDURE Text1.INTERACTIVECHANGE
LOCAL cFilterOld, cFilterIn
cFilterOld = ThisForm.cFilterOld
cFilterIn = KEY()
SET FILTER TO

IF LEFT(This.Value,1) = &quot;*&quot;
IF LEN(ALLTRIM(This.Value)) > 1
ThisForm.cFilter = ;
RIGHT(ALLTRIM(This.Value),LEN(ALLTRIM(This.Value))-1)
IF EMPTY(cFilterOld)
SET FILTER TO ThisForm.cFilter $ &cFilterIn
ELSE
SET FILTER TO &cFilterOld .AND. ;
ThisForm.cFilter $ &cFilterIn
ENDIF
LOCATE
ELSE
IF !EMPTY(cFilterOld)
SET FILTER TO &cFilterOld
ENDIF
ENDIF
ELSE
IF !EMPTY(cFilterOld)
SET FILTER TO &cFilterOld
ENDIF
SEEK ALLT(This.Value)
ENDIF
ThisForm.Refresh()
ThisForm.Grid1.SetFocus
ThisForm.Text1.SetFocus
ENDPROC

PROCEDURE Grid1.AfterRowColChange
LPARAMETERS nColIndex
ThisForm.inRecNo = IIF(EOF() OR BOF(),0,RECNO())
This.Columns(This.ActiveColumn).Text1.BackColor = RGB(255,0,0)
This.Columns(This.ActiveColumn).Text1.DisabledBackColor = RGB(32,224,224)
This.Columns(This.ActiveColumn).Text1.DisabledForeColor = RGB(0,0,0)
This.Refresh()
ThisForm.Text1.SetFocus()
RETURN .T.
ENDPROC

PROCEDURE Grid1.Init
DODEFAULT()
WITH THIS
.SetAll(&quot;DynamicBackColor&quot;, ;
&quot;IIF(recno(This.RecordSource)= ;
ThisForm.inRecno,RGB(32,224,224), ;
RGB(255,255,192))&quot;,&quot;COLUMN&quot;)
.SetAll(&quot;BackColor&quot;, RGB(255,192,192),&quot;Header&quot;)
.SetAll(&quot;Alignment&quot;, 2, &quot;Header&quot;)
.Column1.DynamicBackColor= ;
&quot;IIF('Head' $ cName,RGB(0,255,0),RGB(255,255,255))&quot;
.Column2.DynamicBackColor= ;
&quot;IIF('pits' $ email , RGB(255,0,0), ;
IIF('ramani' $ email, RGB(255,128,255), ;
RGB(255,255,128)))&quot;
ENDWITH
ENDPROC

ENDDEFINE
*
*-- EndDefine: form1
**************************************************
ramani :)
(Subramanian.G),FoxAcc, ramani_g@yahoo.com
Merry Christmas & Happy New Year [bigears] [party] [2thumbsup]

 
kanagha

For another approach see :-

'How can I color code cells in a grid?' faq184-624
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
kanagha

but the problem is that where should I call the &quot;dynamic&quot; function and where should I create the objects of textred and textblue.I want to know the event where should I write?

The problem is that in order to show you how to do this, in this type of forum setting (which has no download facility), we have to post the code. Normally it would be up to you to &quot;translate&quot; the code to make it work for you. But here is step-by-step explanation on how to use my suggestion.
[ol][li]Create a blank form.[/li]
[li]Put a grid on it.[/li]
[li]In the load of the form put:
Code:
CREATE CURSOR myCursor (NAME C(20),COLOR C(10))
INSERT INTO myCursor (NAME,COLOR) VALUES (&quot;MIKE&quot;,&quot;BLUE&quot;)
INSERT INTO myCursor (NAME,COLOR) VALUES (&quot;JOHN&quot;,&quot;RED&quot;)
INSERT INTO myCursor (NAME,COLOR) VALUES (&quot;PAUL&quot;,&quot;YELLOW&quot;)
GO TOP
[/li]
[li]Create a new function for the form (In design mode - New method) and call it &quot;Dynamic&quot; and put the following code in it:
Code:
DO CASE
CASE myCursor.COLOR = &quot;RED&quot;
    RETURN &quot;textRed1&quot;
CASE myCursor.COLOR = &quot;BLUE&quot;
    RETURN &quot;textBlue1&quot;
CASE myCursor.COLOR = &quot;YELLOW&quot;
    RETURN &quot;textYellow1&quot;
ENDCASE
[/li]
[li]
In the init of the grid put:
Code:
WITH THIS
        .column2.DYNAMICCURRENTCONTROL = &quot;dynamic()&quot;
ENDWITH
[/li]
[li]Set the column count of the grid to 2[/li]
[li]Create 3 new textbox classes (change the backcolor of each one of them to red-blue and yellow)[/li]
[li]Add the three textboxes o the column2 of your grid[/li]
[li] If you change values in the grid, then in the valid of column 2 make a call to the grid's init[/li][/ol]

That should do it.








Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Mike

I suspect you need .Sparse = .F. for each column in your last post?
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Chris

I suspect you need .Sparse = .F. for each column in your last post?

Yes, you are right.
Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top