Aug 16th, 2005, 05:46 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Flexgrid Column Width
I have some data going into a felxgrid and need the columnc big enough to be able to see the entire data being entered.
How would i do this?
Cheers
ILMV aka <insert aka name here>
Aug 16th, 2005, 06:32 PM
#2
Re: Flexgrid Column Width
Keep track of the widest data using TextWidth while you are loading it and then size the grid accordingly.
Aug 16th, 2005, 06:38 PM
#3
Thread Starter
Frenzied Member
Re: Flexgrid Column Width
Ok then, i will try that.
Cheeers - IMLV
Aug 16th, 2005, 06:47 PM
#4
Re: Flexgrid Column Width
Here is a routine I use to adjust the HEIGHT of FlexGrid row in order to accomodate variable text (this would go in a module) :
VB Code:
'-----------------------------------------------------------------------------
Public Sub AdjustGridRowHeight(pobjGrid As MSFlexGrid, _
pobjLabel As Label, _
plngRow As Long, _
plngCol As Long, _
Optional pblnAddNewLine As Boolean = False)
'-----------------------------------------------------------------------------
' Adjusts the height of a flexgrid row, using the height required by a
' label using the same font, fontsize, and width. The label used must have
' Autosize and WordWrap set to True.
' Also, the WordWrap property of the flexgrid must be set to True.
With pobjGrid
.Row = plngRow
.Col = plngCol
pobjLabel.FontName = .CellFontName
pobjLabel.FontSize = .CellFontSize
pobjLabel.Width = .CellWidth
pobjLabel.Caption = .Text & IIf(pblnAddNewLine, vbNewLine, "")
If pobjLabel.Height > .RowHeight(.Row) Then
.RowHeight(.Row) = pobjLabel.Height
End If
End With
End Sub
' This is another routine I use sometimes to assign text to a cell
'-----------------------------------------------------------------------------
Public Sub SetCellText(pobjGrid As MSFlexGrid, _
plngRow As Long, _
plngCol As Long, _
pstrText As String, _
Optional pblnBold As Boolean = False, _
Optional plngAlignment _
As MSFlexGridLib.AlignmentSettings = flexAlignLeftTop)
'-----------------------------------------------------------------------------
With pobjGrid
.Row = plngRow
.Col = plngCol
.Text = pstrText
.CellFontBold = pblnBold
.CellAlignment = plngAlignment
End With
End Sub
In a form - need a FlexGrid and a Label. Assumes the following:
GRID
(1) grid is called grdNotes
(2) FixedCols set to 0
(3) WordWrap set to True
LABEL
(1) Named lblCellText
(2) AutoSize set to True
(3) WordWrap set to True
(4) Visible set to False
VB Code:
Private Sub Form_Load()
grdNotes.Redraw = False
With grdNotes
.Rows = 1
.Cols = 3
.ColWidth(0) = .Width \ 3
.ColWidth(1) = .Width \ 3
.ColWidth(2) = .Width \ 3
End With
SetCellText grdNotes, 0, 0, "Date/Time", True
SetCellText grdNotes, 0, 1, "Contact", True
SetCellText grdNotes, 0, 2, "Notes", True
' Typically, you would load the grid in by looping thru a recordset or file.
' In this example, 3 records are loaded "manually" ...
grdNotes.Rows = grdNotes.Rows + 1
SetCellText grdNotes, 1, 0, "8/10/2005"
SetCellText grdNotes, 1, 1, "John Smith"
SetCellText grdNotes, 1, 2, "j asdf dsflaks jfalskjf lasjdfaslfj asdfjasdfjsadf asdfasdf" _
& "asdf asdf asdfasdfkasdf aslkfas;ldfkasldfkasf werw er wdf " _
& "dsaf wer wdf awr adf aserfawdf;la mcvas;ldkas;rkwerwer"
AdjustGridRowHeight grdNotes, lblCellText, 1, 2
grdNotes.Rows = grdNotes.Rows + 1
SetCellText grdNotes, 2, 0, "8/11/2005"
SetCellText grdNotes, 2, 1, "Bob Jones"
SetCellText grdNotes, 2, 2, "j asdf dsflaks jfalskjf lasjdfaslfj asdfjasdfjsadf asdfasdf"
AdjustGridRowHeight grdNotes, lblCellText, 2, 2
grdNotes.Rows = grdNotes.Rows + 1
SetCellText grdNotes, 3, 0, "8/13/2005"
SetCellText grdNotes, 3, 1, "Mary Belle"
SetCellText grdNotes, 3, 2, "j asdf dsflaks jfalskjf lasjdfaslfj asdfjasdfjsadf asdfasdf" _
& "asdf asdf asdfasdfkasdf aslkfas;ldfkasldfkasf werw er wdf " _
& "dsaf wer wdf awr adf aserfawdf;la mcvas;ldkas;rkwerwer" _
& "asdf asdf asdfasdfkasdf aslkfas;ldfkasldfkasf werw er wdf " _
& "dsaf wer wdf awr adf aserfawdf;la mcvas;ldkas;rkwerwer"
AdjustGridRowHeight grdNotes, lblCellText, 3, 2
grdNotes.Redraw = True
End Sub
For good measure, I attached a sample project using the above code. I hope this helps.
Attached Files
"It's cold gin time again ..."
Check out my website
here .
Aug 18th, 2005, 10:12 AM
#5
Thread Starter
Frenzied Member
Re: Flexgrid Column Width
VB Code:
Public Sub DisplayGrid()
Dim i, j As Single
If MySearch.RecordCount > 0 Then 'Checks to see if greater than zero
MySearch.MoveLast 'Moves to last record in recordset
MySearch.MoveFirst 'And then moves to first record in recordset
'Below: Dynamically size flexgrid to recordset size
MSFlexGrid1.Cols = (MySearch.Fields.Count)
MSFlexGrid1.Rows = MySearch.RecordCount + 1
'Below: Populate column headings with field names
MSFlexGrid1.Row = 0
For i = 0 To MySearch.Fields.Count - 1
MSFlexGrid1.Col = i
MSFlexGrid1.Text = MySearch.Fields(i).Name
MSFlexGrid1.ColWidth(i) = Len(MSFlexGrid1.Text) * ColWidthRatio
Next i
'Below: Populate flexgrid with recordset data
For i = 0 To MySearch.RecordCount - 1
MSFlexGrid1.Row = i + 1
For j = 0 To MySearch.Fields.Count - 1
MSFlexGrid1.Col = j
If Not MySearch.Fields(j) = " " Then
MSFlexGrid1.Text = MySearch.Fields(j)
If MSFlexGrid1.ColWidth(j) < Len(MSFlexGrid1.Text) * ColWidthRatio Then MSFlexGrid1.ColWidth(j) = Len(MSFlexGrid1.Text) * ColWidthRatio
Else
MSFlexGrid1.Text = " "
End If
Next j
MySearch.MoveNext
Next i
Else
MsgBox "There are no records for the parameter you selected", vbExclamation, "Grid Populate Error"
'Displays message box with OK button and Exclamation icon.
End If
SQLTXT = "" 'Set recordset to nothing to be safe
Call FormatFlex
End Sub
This is the code i am currently using, provided by my teacher, but i thinks its crap because if the width is smaller then the flexgrid width (for that column)
it doesnt just make it just big enough, it goes further, making the entire width of the flexgrid huge, when it doesnt have to be.
And that isnt very user friendly, having to scroll all the way across to see the information.
How can i change this, so that it wuold still check to see if the column is big enough, but if it isnt make the widht just big enough?
Thank you
ILMv
Aug 18th, 2005, 11:03 AM
#6
Thread Starter
Frenzied Member
Re: Flexgrid Column Width
Anyone?
ILMV
Aug 18th, 2005, 12:41 PM
#7
Re: Flexgrid Column Width
Hey ILMV,
I have adjusted my example to include a new routine "AdjustGridColWidth". This should help you do what you are looking for. (Note that in addition to the use of this new routine, the WordWrap property of both the Label and the grid must now = False.)
Attached Files
"It's cold gin time again ..."
Check out my website
here .
Aug 18th, 2005, 06:01 PM
#8
Thread Starter
Frenzied Member
Re: Flexgrid Column Width
cheers BruceG, but i have problems, i have put the sub's in my global module
VB Code:
For i = 0 To MySearch.RecordCount - 1
MSFlexGrid1.Row = i + 1
For j = 0 To MySearch.Fields.Count - 1
MSFlexGrid1.Col = j
If Not MySearch.Fields(j) = " " Then
MSFlexGrid1.Redraw = False
SetCellText MSFlexGrid1, [B]i, j[/B], MySearch.Fields(j)
AdjustGridColWidth MSFlexGrid1, lblCellText, [B]i, j[/B]
MSFlexGrid1.Redraw = True
'MSFlexGrid1.Text = MySearch.Fields(j)
'AdjustGridColWidth MSFlexGrid1, lblCellText, i, j
'If MSFlexGrid1.ColWidth(j) < Len(MSFlexGrid1.Text) * ColWidthRatio Then MSFlexGrid1.ColWidth(j) = Len(MSFlexGrid1.Text) * ColWidthRatio
Else
MSFlexGrid1.Text = " "
End If
Next j
MySearch.MoveNext
Next i
But i get an error on the i, and j letters here. (bold)
Aug 18th, 2005, 06:14 PM
#9
Re: Flexgrid Column Width
Aug 18th, 2005, 06:17 PM
#10
Thread Starter
Frenzied Member
Re: Flexgrid Column Width
Oh, sorry it is sorted now. It was because i didnt know exactly what was going on, but after alot of head banging andd staring ar GBruceyG's code, i know no what the hell it does Cheers Bruce, and everyone for your replies, much appreciated.
Aug 18th, 2005, 06:27 PM
#11
Re: [RESOLVED] Flexgrid Column Width
Headbanging helps most of the times
Aug 18th, 2005, 07:09 PM
#12
Re: [RESOLVED] Flexgrid Column Width
So does dimming your variables.
Aug 18th, 2005, 09:07 PM
#13
Re: [RESOLVED] Flexgrid Column Width
Glad u worked it out.
"It's cold gin time again ..."
Check out my website
here .
Aug 19th, 2005, 04:19 AM
#14
Thread Starter
Frenzied Member
Re: [RESOLVED] Flexgrid Column Width
well spotted MLiss, but they were further up the page
Cheers for your help bruce, we got about 40 people doing this at college, and they are all doing it the stupid way, god help them
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width