Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to auto size grid columns
Message
 
 
To
03/10/2004 18:57:57
Michael Ouellette
Australian Technical Services (VIC) Pty
Australia
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00948300
Message ID:
00948535
Views:
21
>I only have one column in the grid class
>I add columns based on the cursor passed.
>It looks like I can apply the example mentioned by Martin.
>
>Thanks all

Hi Michael,

Haven't checked the methods recommended by others, but here is my old code for the same purpose from our grid class. Hopefully it's still useful:
********************************************************************
*  Description.......: wgGrid.FormatAndSetCaption - format all grid columns and set caption to be in Proper format
*  Calling Samples...:
*  Parameter List....:
*  Created by........: Nadya Nosonovsky 03/09/2000 02:03:22 PM
*  Modified by.......: Nadya Nosonovsky 03/29/2000 01:37:26 PM
********************************************************************
local loColumn, loControl, lnWidthTotal, lnSel,  ;
	lnFWidth, lnCWidth, lcFieldName, lcFieldType

lnSel=select() && save current area

if this.recordsourcetype=1 OR this.recordsourcetype=0 && Alias or table
	select (this.recordsource)
	lnWidthTotal= iif(this.recordmark, 10,0)+ iif(this.scrollbars>1,sysmetric(5),0) ;
		+ 2 * this.gridlinewidth + iif(this.deletemark, 10,0)+10 && record marker & vertical scroll bar+deletion mark
	for each loColumn in this.columns
		* -- For each column determine the width and use the correct caption.  
		* -- Display the caption via the proper() function.
		lcFieldName=alltrim(substr(loColumn.controlsource,rat('.',loColumn.controlsource)+1))
		lcFieldType=type(lcFieldName)
		lnFWidth=iif(lcFieldType='D',13,iif(lcFieldType='T',28,fsize(lcFieldName)+3)) && Field size
		for each loControl in loColumn.controls
			if lower(loControl.baseclass) = "header"
				* Handle condition of header set in grid.
				if lower(left(loControl.caption,6))='header'  && Don't override a preset caption
					* -- NSL Use the DBC caption if it exists.
					loControl.caption=dbgetprop(loColumn.controlsource,'field','caption')  
				endif
				if empty(loControl.caption)  && There is no DBC caption, Just use the Field name
					loControl.caption=proper(lcFieldName)
				else
					loControl.caption=proper(loControl.caption)  && If it has been coded in the grid, use it.
				endif

				lnCWidth=(txtwidth(loControl.caption,loControl.fontname,loControl.fontsize)+2) * ;
					fontmetric(6,loControl.fontname,loControl.fontsize)+3 && Width of caption
			endif
			exit  && No need to ck the other controls
		endfor

		loColumn.width=round(max(lnFWidth*fontmetric(6,loColumn.fontname,loColumn.fontsize),lnCWidth),0)
		lnWidthTotal=lnWidthTotal+loColumn.width

** Nadya Nosonovsky 03/29/2000 01:35:47 PM added Incremental Search (could be added for ReadOnly grid)

		if this.lIncremental and lcFieldType='C' && We would use this feature only for char fields
			if lower(loColumn.currentcontrol)='text1' && standard text1 control
				loColumn.removeobject('text1')
				loColumn.newobject('text1','ingrid','wgcontrols.vcx') && Incremental search textbox
				loColumn.text1.visible=.t.
				*loColumn.ReadOnly=.t. && this is necessary condition	
				loColumn.text1.cLocateExpression='upper('+lcFieldName+')'
			endif
		endif

*  Nadya Nosonovsky 03/29/2000 01:36:17 PM added Show Memo

		if this.lShowMemo and lcFieldType='M' && We want to show Memo field
			if lower(loColumn.currentcontrol)='text1' && standard text1 control
				loColumn.removeobject('text1')
				loColumn.newobject('text1','edtexpand','wgcontrols.vcx') && expandable editbox
				loColumn.text1.visible=.t.
				loColumn.sparse=.f.
			endif
		endif
	endfor

	this.width=min(this.width,lnWidthTotal)
endif
select (lnSel) && return to previous area
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform