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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternating row colors for a grid 2

Status
Not open for further replies.

ugagrad

Programmer
Sep 12, 2003
49
US
Hello all,
I am trying to alternate colors in my grid. However, we are using a cursor that has many index tags. So whenever we do set order to the order of the grid changes. so the following code will not work
SELECT curDBVIEW
THISFORM.Grid1.SetAll("DynamicBackColor",;
"IIF(MOD(RECNO(), 2)=0, RGB(255,255,255) ;
, RGB(220,220,220))", "Column")

I have tried a few approaches to this but I have had no success. Does anyone have any suggestions.

Thanks in advance.

 
ugagrad

If you are using the column headers to reset the order, try this in the click event of each column header:
Code:
this.parent.parent.Init(SUBSTR(this.parent.ControlSource,AT(".",this.parent.ControlSource)+1))

And this in the init of the grid:
Code:
Parameters tcOrder,tcWhere,tlColumnFieldOnly

Local laColumnCS(This.ColumnCount)  As Character ,;
	lcGridRS As Character,;
	liGridRST As Integer ,;
	lcSltTable As Character,;
	liCol As Integer ,;
	lcDoSelect As Character,;
	lcSafe
	
this.Tag = IIF( EMPTY(this.Tag),this.RecordSource,this.Tag)

For liCol = 1 To This.ColumnCount
	laColumnCS(liCol) = ;
		SUBSTR(this.columns(m.liCol).controlsource,AT(".",This.Columns(liCol).ControlSource)+1)
Endfor

m.liGridRST= This.RecordSourceType

m.lcSltTable = "slt"+PROPER(This.RecordSource)
If Used(lcSltTable)  
	Use In (m.lcSltTable)
Endif
Local lcSltFld
m.lcDoSelect = ""
If m.tlColumnFieldOnly
	For i = 1 To This.ColumnCount
		m.lcSltFld = m.lcSltFld +Iif(Empty(m.lcSltFld),"",",")+m.laColumnCS(i)
	Endfor
ENDIF

m.lcSafe = "SET SAFETY "+ SET("safe")
SET SAFETY OFF
m.lcDoSelect = ;
	"select "+;
	IIF(m.tlColumnFieldOnly,m.lcSltFld,"*")+" from "+this.Tag +;
	IIF(EMPTY(m.tcWhere)," "," Where "+tcWhere) + ;
	IIF(EMPTY(m.tcOrder)," "," Order by "+m.tcOrder)+ ;
	" into table "+m.lcSltTable

&lcDoSelect
&lcSafe

SELECT(m.lcSltTable)
	
this.RecordSource = m.lcSltTable
this.RecordSourceType = m.liGridRST
FOR m.liCol = 1 TO this.ColumnCount
	this.Columns(m.liCol).controlsource= m.lcSltTable+"."+m.laColumnCS(m.liCol)
ENDFOR
this.SetAll("DynamicBackColor","IIF(RECNO()%2=0,RGB(255,200,0),RGB(255,255,255))","column")

Sorry it is so long, but it works for me.






Mike Gagnon

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

I can see what your code is going to achieve, but -- just out of curiosity -- why are you putting it in the grid's Init? It seems odd to me that the Init code will be called as the result of the user clicking on a column head. I'd have been inclined to write a separate method to handle the job, and to call that method from both the Init and the column heads' clicks.

Just a thought.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Mike

Yes you are right, but if I put it in the init, the function get triggered when the form first load, without having to call for it. But a separate method would do as well, but then in the init of the grid, you would have to make a call to it, to get the alternating rows on first instaciation. Just an extra line of code...


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
thanks mgagnon,
that worked beautifully
 
One other thing, I have multiple forms where I am using this code. However, on a few of them the cursor is created in the Init method of the form. It seems that the Init method of the grid is called before the forms init method begins execution. Is there anyway around this problem or do I need to rewrite where my cursor is created.

Thanks in advance,
 
However, on a few of them the cursor is created in the Init method of the form.

If nothing in the form's properties are needed to produce the cursor, move this to the load of the form, which is first in the sequence of instanciation.




Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
thanks for all of your help magnon,
that simple fixed worked for a majority of my forms however, one in particualar(which just happens to be the main form, go figure) uses parameters passed to it from a prg to determine what data the cursor will contain. In the load event those parameters don't seem to be accessible. Do you have any suggestions, if not thank you anyway you have been a great help.

Thanks again
 
In that case move the call to the function lower down on the chain. The loading sequence of a form is [blue]L[/blue]oad [blue]I[/blue]nit [blue]S[/blue]etFocus [blue]A[/blue]ctivate [blue]G[/blue]otFocus.
Remeber LISA G ;-)


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
VFP9 (or was it 8?) adds the "BindControls" property that you can set to .F. at design time, then create your cursor in THISFORM.Init (where you can access the parameters), and then THISFORM.BindControls=.T. to bind the grid to the cursor (and all other controls to their data).

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top