| {\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}} {\colortbl\red0\green0\blue0;} \deflang1044\pard\plain\f0\fs17 Automatically resize the columns in any flex grid to give a nice, professional appearance. \par \par \par Public Sub automatically resizes MS Flex Grid columns to match the width of the text, no matter the size of the grid or the number of columns. \par \par Reads first n number of rows of data, and adjusts column size To match the widest cell of text. Will even expand columns proportionately If they aren't wide enough to fill out the entire width of the grid. Configurable constraints allow you to designate \par 1) Any flex grid To resize \par 2) Maximum column width \par 3) the maximum number of rows In depth To look For the widest cell of text. \par \par Inputs:msFG (MSFlexGrid) = The name of the flex grid to resize .... MaxRowsToParse (integer) = The maximum number of rows (depth) of the table to scan for cell width (e.g. 50) .... MaxColWidth (Integer) = The maximum width of any given cell in twips (e.g. 5000) \par \par Assumes:Simply drop this public sub in to your form or module and access it from anywhere in your program to automatically resize any flex grid. \par \par Public Sub AutosizeGridColumns(ByRef msFG As MSFlexGrid, ByVal MaxRowsToParse As Integer, _ \par ByVal MaxColWidth As Integer) \par Dim I, J As Integer \par Dim txtString As String \par Dim intTempWidth, BiggestWidth As Integer \par Dim intRows As Integer \par Const intPadding = 150 \par \par With msFG \par \par For I = 0 To .Cols - 1 \par ' Loops through every column \par .Col = I \par ' Set the active colunm \par intRows = .Rows \par ' Set the number of rows \par If intRows > MaxRowsToParse Then intRows = MaxRowsToParse \par ' If there are more rows of data, reset intRows to the MaxRowsToParse constant \par \par intBiggestWidth = 0 \par ' Reset some values to 0 \par \par For J = 0 To intRows - 1 \par ' check up to MaxRowsToParse # of rows and obtain the greatest width of the cell contents \par .Row = J \par txtString = .Text \par intTempWidth = TextWidth(txtString) + intPadding \par ' The intPadding constant compensates for text insets You can adjust this value above as desired. \par \par If intTempWidth > intBiggestWidth Then intBiggestWidth = intTempWidth \par ' Reset intBiggestWidth to the intMaxColWidth value if necessary \par Next J \par .ColWidth(I) = intBiggestWidth \par Next I \par ' Now check to see if the columns aren't as wide as the grid itself. \par ' If not, determine the difference and expand each column proportionately \par ' to fill the grid \par intTempWidth = 0 \par \par For I = 0 To .Cols - 1 \par intTempWidth = intTempWidth + .ColWidth(I) \par ' Add up the width of all the columns \par Next I \par \par If intTempWidth < msFG.Width Then \par ' Compate the width of the columns to the width of the grid control \par ' and if necessary expand the columns. \par intTempWidth = Fix((msFG.Width - intTempWidth) / .Cols) \par ' Determine the amount od width expansion needed by each column \par \par For I = 0 To .Cols - 1 \par .ColWidth(I) = .ColWidth(I) + intTempWidth \par ' add the necessary width to each column \par \par Next I \par End If \par End With \par End Sub \par } |
Auto resize flexgrid column widths |
India web developer web development India | India web development company India ecommerce web developer