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>
Printable View
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>
Keep track of the widest data using TextWidth while you are loading it and then size the grid accordingly.
Ok then, i will try that.
Cheeers - IMLV
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.
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
Anyone? :D
ILMV
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.)
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)
What does the error say?
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 :D Cheers Bruce, and everyone for your replies, much appreciated.
Headbanging helps most of the times :D
So does dimming your variables.
Glad u worked it out. :)
;) well spotted MLiss, but they were further up the page :D
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