Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Cetin Basoz
  Where is Cetin Basoz?
 Cetin Basoz
 To: Gimhan 83
  Where is Gimhan 83?
 Sri Lanka
 Gimhan 83
Subject: RE: Highlight row
Thread ID: 373327 Message ID: 373344 # Views: 60 # Ratings: 3
Version: Visual FoxPro 7 Category: VFP and .NET
Date: Thursday, March 21, 2013 1:31:17 PM         

> Hi experts,
> Is there any specific way to highlight a specific row in a grid with a different color.
> And can't we highlight a specific cell (row,col) in a vfp grid?
> Urgent reply much appreciate. Thanks.

You can highlight a complete row, a specific cell etc based on the conditions using DynamicBackColor and DynamicForeColor properties. In those properties your color as an expression and that expression is reevaluated for the "visible" cells in each refresh. When you say a specific cell, then it suggests the condition might be complex. Then it would be better to create a form level custom method that would return the color.

Here is a sample that does row coloring based on emp_id:

Public oForm
oForm = Createobject('myForm')

Define Class myForm As Form
  DataSession = 2

  Add Object myGrid As Grid With ;
    RecordSource='Orders',  Height=400,  Width=400,Anchor=15, ReadOnly=.T.

  Procedure Load
    Use (_samples+'data\orders')

  Procedure Init
    This.myGrid.SetAll('DynamicBackColor', '(thisform.ColorMe(Orders.Emp_id, .F.))')
    This.myGrid.SetAll('DynamicForeColor', '(thisform.ColorMe(Orders.Emp_id, .T.))')

  Procedure ColorMe(tcValue, tlForeColor)
    Local lnBackColor, lnForeColor
    lnBackColor = Bitand( Val(Sys(2007,m.tcValue,1,1)), 0x00FFFFFF )
    If !m.tlForeColor
      Return m.lnBackColor

    Local lnRed, lnGreen, lnBlue,lnAlpha
    lnRed = Bitand(Bitrshift(m.lnBackColor,0),0xFF)
    lnGreen = Bitand(Bitrshift(m.lnBackColor,8),0xFF)
    lnBlue = Bitand(Bitrshift(m.lnBackColor,16),0xFF)
    lnAlpha = (Max(m.lnRed,m.lnGreen,m.lnBlue)+Min(m.lnRed,m.lnGreen,m.lnBlue))/2

    Return Iif(m.lnAlpha < 0x80, 0xFFFFFF, 0)

And here is a sample that does coloring at cell level:

Public oForm
oForm = Createobject("myForm")

Define Class myForm As Form
  DataSession = 2
  Height = 400
  Width = 600

  Add Object myGrid As Grid With ;
    Height = 400, Width = 600, Anchor=15,RecordSource="employee"

  Procedure Load
    Use employee

  Procedure Init
    For Each oColumn In This.myGrid.Columns
      oColumn.DynamicBackColor = "thisform.MyColColor(["+m.oColumn.ControlSource+"])"

  Procedure myColColor
    Lparameters tcControlSource
    Local luValue, lnColor
    lnColor = 0xFFFFFF && default white
    * If working with us for more than 20 years
*!*	    * Return same color for all columns
*!*	    If employee.Hire_date < Gomonth(Date(),-12*18)
*!*	      Return Rgb(192,192,192)
*!*	    Endif
    * Return same color for all columns except title and birth_date
    * set title and birth_date as separate
    * If employee.Hire_date is hardcoded and would return default gray
    * when calling grid's recordsource is not employee.
    * Then it could also be checked as:
    * lcAlias = juststem(m.tcControlSource) 
    * lcField = justext(m.tcControlSource)  
    * To differentiate from a calculated value expression you might do something like:
    * Used(m.lcAlias) and fsize(m.lcField,m.lcAlias)>0
    * Calculated values are likely to fail in a grid w/o parentheses.
    If employee.Hire_date < Gomonth(Date(),-12*20) && working for more than 20 years with us
      lnColor = Rgb(192,192,192) && default gray

    If Type(m.tcControlSource) = 'G'
      Return m.lnColor
    luValue = Evaluate(m.tcControlSource)
    Do Case
      Case Lower(m.tcControlSource) = "employee.title"
        If Lower(m.luValue) = "sales manager"
          lnColor = Rgb(255,255,0)
      Case Lower(m.tcControlSource) = "employee.birth_date"
        If Year(m.luValue) % 2 = 0
          lnColor = Rgb(0,255,255)
    Return m.lnColor

Cetin Basoz

.Net has got better.Think about moving - check my blog:
My Blog

MongoDb Certified Developer
MongoDb Certified DBA

Support Wikipedia


Highlight row Posted by nayana82 Abeykoon @ 3/21/2013 9:24:06 AM
RE: Highlight row Posted by llyrad atsituab @ 3/21/2013 9:37:58 AM
RE: Highlight row Posted by nayana82 Abeykoon @ 3/21/2013 10:33:44 AM
RE: Highlight row Posted by Stefan Wuebbe @ 3/21/2013 12:29:47 PM
RE: Highlight row Posted by Cetin Basoz @ 3/21/2013 1:31:17 PM