How to resize the column width in DBGrid? 
Author Message
 How to resize the column width in DBGrid?

Hi,
  just wondering if anyone knows how do I resize the column width in VB
code? what happens was, i am using DBGrid to call up some data from the
Access db. I managed to get the data displayed on the DBGrid, but some
of the column widths are too wide. I know i can manually adjust the
width during runtime, but it'll revert back to the old preset width when
i rerun the program.
  Anyone knows how to i do the width adjustment in the VB code so that
everytime i launch my program, the DBGrid's widths will be those that i
set in the code?

Any help is appreciated. Thanks in advance.

ch



Tue, 28 Sep 2004 08:23:01 GMT  
 How to resize the column width in DBGrid?
These's no magic technique to get it to automatically set itself to
the width of its widest entry (if only!) so you have to work it out
for yourself. I'll assume you're using ADO.

1. Quick and Dirty
Each Field has a DefinedSize property which returns the maximum number
of characters any value in that column could have. You can use that
and the form's TextWidth method to get a width for the column:

    Dim lngCol          As Long
    Dim lngColumnWidth  As Long
    Dim lngCaptionWidth As Long

    For lngCol = 0 To rst.Fields.Count
        lngColumnWidth = Me.TextWidth(String$(objField.DefinedSize,
"M"))
        lngCaptionWidth =
Me.TextWidth(DBGrid1.Columns(lngCol).Caption))
        If lngCaptionWidth > lngColumnWidth Then
            lngColumnWidth = lngCaptionWidth
        End If
        DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
    Next lngCol

2. Better-Looking But More Work
Loop through all the rows in the recordset and use the ActualSize
property to get the string lengths of all the entries in that field.
Keep a note of the longest length and then use the TextWidth method to
convert it to a column width.

    Dim lngCol          As Long
    Dim lngColumnWidth  As Long
    Dim objField        As ADODB.Field

    For lngCol = 0 To rst.Fields.Count
        lngColumnWidth = Len(DBGrid1.Columns(lngCol).Caption))
        Set objField = rst.Fields(lngCol)
        rst.MoveFirst
        Do Until rst.EOF
            If objField.ActualSize > lngColumnWidth Then
                lngColumnWidth = objField.ActualSize
            End If
            rst.MoveNext
        Loop
        lngColumnWidth = Me.TextWidth(String$(lngColumnWidth, "M"))
        DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
    Next lngCol

This is quick enough with up to about 100 rows but you can refine it
by avoiding the loop for certain columns, depending on the data type.
For example a date column will be as wide as "88/88/8888" (or "88 MMM
8888" or whatever), unless the column heading is wider in which case
you'd use that. If you know the column is a number no bigger that
"1,000,000.00" you could use that.

NB: The Me.TextWidth method needs the form and the grid to have the
same font.



Tue, 28 Sep 2004 16:58:25 GMT  
 How to resize the column width in DBGrid?
Thanks for the tips. I tried it out and I couldnt figured out where the
'objField' is defined, in method #1. Some explanations would be
appreciated. Thanks.
Quote:

> These's no magic technique to get it to automatically set itself to
> the width of its widest entry (if only!) so you have to work it out
> for yourself. I'll assume you're using ADO.

> 1. Quick and Dirty
> Each Field has a DefinedSize property which returns the maximum number
> of characters any value in that column could have. You can use that
> and the form's TextWidth method to get a width for the column:

>     Dim lngCol          As Long
>     Dim lngColumnWidth  As Long
>     Dim lngCaptionWidth As Long

>     For lngCol = 0 To rst.Fields.Count
>         lngColumnWidth = Me.TextWidth(String$(objField.DefinedSize,
> "M"))
>         lngCaptionWidth =
> Me.TextWidth(DBGrid1.Columns(lngCol).Caption))
>         If lngCaptionWidth > lngColumnWidth Then
>             lngColumnWidth = lngCaptionWidth
>         End If
>         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
>     Next lngCol

> 2. Better-Looking But More Work
> Loop through all the rows in the recordset and use the ActualSize
> property to get the string lengths of all the entries in that field.
> Keep a note of the longest length and then use the TextWidth method to
> convert it to a column width.

>     Dim lngCol          As Long
>     Dim lngColumnWidth  As Long
>     Dim objField        As ADODB.Field

>     For lngCol = 0 To rst.Fields.Count
>         lngColumnWidth = Len(DBGrid1.Columns(lngCol).Caption))
>         Set objField = rst.Fields(lngCol)
>         rst.MoveFirst
>         Do Until rst.EOF
>             If objField.ActualSize > lngColumnWidth Then
>                 lngColumnWidth = objField.ActualSize
>             End If
>             rst.MoveNext
>         Loop
>         lngColumnWidth = Me.TextWidth(String$(lngColumnWidth, "M"))
>         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
>     Next lngCol

> This is quick enough with up to about 100 rows but you can refine it
> by avoiding the loop for certain columns, depending on the data type.
> For example a date column will be as wide as "88/88/8888" (or "88 MMM
> 8888" or whatever), unless the column heading is wider in which case
> you'd use that. If you know the column is a number no bigger that
> "1,000,000.00" you could use that.

> NB: The Me.TextWidth method needs the form and the grid to have the
> same font.



Fri, 01 Oct 2004 09:20:15 GMT  
 How to resize the column width in DBGrid?
Thanks for the tips. I tried it out and I couldnt figured out where the
'objField' is defined, in method #1. Some explanations would be
appreciated. Thanks.
Quote:

> These's no magic technique to get it to automatically set itself to
> the width of its widest entry (if only!) so you have to work it out
> for yourself. I'll assume you're using ADO.

> 1. Quick and Dirty
> Each Field has a DefinedSize property which returns the maximum number
> of characters any value in that column could have. You can use that
> and the form's TextWidth method to get a width for the column:

>     Dim lngCol          As Long
>     Dim lngColumnWidth  As Long
>     Dim lngCaptionWidth As Long

>     For lngCol = 0 To rst.Fields.Count
>         lngColumnWidth = Me.TextWidth(String$(objField.DefinedSize,
> "M"))
>         lngCaptionWidth =
> Me.TextWidth(DBGrid1.Columns(lngCol).Caption))
>         If lngCaptionWidth > lngColumnWidth Then
>             lngColumnWidth = lngCaptionWidth
>         End If
>         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
>     Next lngCol

> 2. Better-Looking But More Work
> Loop through all the rows in the recordset and use the ActualSize
> property to get the string lengths of all the entries in that field.
> Keep a note of the longest length and then use the TextWidth method to
> convert it to a column width.

>     Dim lngCol          As Long
>     Dim lngColumnWidth  As Long
>     Dim objField        As ADODB.Field

>     For lngCol = 0 To rst.Fields.Count
>         lngColumnWidth = Len(DBGrid1.Columns(lngCol).Caption))
>         Set objField = rst.Fields(lngCol)
>         rst.MoveFirst
>         Do Until rst.EOF
>             If objField.ActualSize > lngColumnWidth Then
>                 lngColumnWidth = objField.ActualSize
>             End If
>             rst.MoveNext
>         Loop
>         lngColumnWidth = Me.TextWidth(String$(lngColumnWidth, "M"))
>         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
>     Next lngCol

> This is quick enough with up to about 100 rows but you can refine it
> by avoiding the loop for certain columns, depending on the data type.
> For example a date column will be as wide as "88/88/8888" (or "88 MMM
> 8888" or whatever), unless the column heading is wider in which case
> you'd use that. If you know the column is a number no bigger that
> "1,000,000.00" you could use that.

> NB: The Me.TextWidth method needs the form and the grid to have the
> same font.



Fri, 01 Oct 2004 09:20:59 GMT  
 How to resize the column width in DBGrid?
Oops! Sorry, I missed that bit. Missing lines inserted:

     Dim lngCol          As Long
     Dim objField        As ADODB.Field
     Dim lngColumnWidth  As Long
     Dim lngCaptionWidth As Long

     For lngCol = 0 To rst.Fields.Count
         Set objField = rst.Fields(lngCol)
         lngColumnWidth = Me.TextWidth(String$(objField.DefinedSize, "M"))
         lngCaptionWidth = Me.TextWidth(DBGrid1.Columns(lngCol).Caption))
         If lngCaptionWidth > lngColumnWidth Then
             lngColumnWidth = lngCaptionWidth
         End If
         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
     Next lngCol

Quote:

> Thanks for the tips. I tried it out and I couldnt figured out where the
> 'objField' is defined, in method #1. Some explanations would be
> appreciated. Thanks.


> > These's no magic technique to get it to automatically set itself to
> > the width of its widest entry (if only!) so you have to work it out
> > for yourself. I'll assume you're using ADO.

> > 1. Quick and Dirty
> > Each Field has a DefinedSize property which returns the maximum number
> > of characters any value in that column could have. You can use that
> > and the form's TextWidth method to get a width for the column:

> >     Dim lngCol          As Long
> >     Dim lngColumnWidth  As Long
> >     Dim lngCaptionWidth As Long

> >     For lngCol = 0 To rst.Fields.Count
> >         lngColumnWidth = Me.TextWidth(String$(objField.DefinedSize,
> > "M"))
> >         lngCaptionWidth =
> > Me.TextWidth(DBGrid1.Columns(lngCol).Caption))
> >         If lngCaptionWidth > lngColumnWidth Then
> >             lngColumnWidth = lngCaptionWidth
> >         End If
> >         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
> >     Next lngCol

> > 2. Better-Looking But More Work
> > Loop through all the rows in the recordset and use the ActualSize
> > property to get the string lengths of all the entries in that field.
> > Keep a note of the longest length and then use the TextWidth method to
> > convert it to a column width.

> >     Dim lngCol          As Long
> >     Dim lngColumnWidth  As Long
> >     Dim objField        As ADODB.Field

> >     For lngCol = 0 To rst.Fields.Count
> >         lngColumnWidth = Len(DBGrid1.Columns(lngCol).Caption))
> >         Set objField = rst.Fields(lngCol)
> >         rst.MoveFirst
> >         Do Until rst.EOF
> >             If objField.ActualSize > lngColumnWidth Then
> >                 lngColumnWidth = objField.ActualSize
> >             End If
> >             rst.MoveNext
> >         Loop
> >         lngColumnWidth = Me.TextWidth(String$(lngColumnWidth, "M"))
> >         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
> >     Next lngCol

> > This is quick enough with up to about 100 rows but you can refine it
> > by avoiding the loop for certain columns, depending on the data type.
> > For example a date column will be as wide as "88/88/8888" (or "88 MMM
> > 8888" or whatever), unless the column heading is wider in which case
> > you'd use that. If you know the column is a number no bigger that
> > "1,000,000.00" you could use that.

> > NB: The Me.TextWidth method needs the form and the grid to have the
> > same font.



Fri, 01 Oct 2004 15:43:19 GMT  
 How to resize the column width in DBGrid?
I tried it out, and I got different errors....errors like 'rst.Fields.Count not
defined', etc.
When i tried to change the width in the layout, and run the program, the data dont
apprear anymore on the grid. Anyway, I found out that the column widths are restricted
by the size of the field defined in MS Access. For instance, if i defined a field, say
quantity, to have a size of 'long double', the width on the DBGrid will be 'long
double'. When i change the field size to byte, the width on the DBGrid will
automatically become narrower.

Anyone can explain this? Is it possible to "programatically" control the width of the
DBGrid regardless of the size of the parameter defined?

thanks.

Quote:

> Oops! Sorry, I missed that bit. Missing lines inserted:

>      Dim lngCol          As Long
>      Dim objField        As ADODB.Field
>      Dim lngColumnWidth  As Long
>      Dim lngCaptionWidth As Long

>      For lngCol = 0 To rst.Fields.Count
>          Set objField = rst.Fields(lngCol)
>          lngColumnWidth = Me.TextWidth(String$(objField.DefinedSize, "M"))
>          lngCaptionWidth = Me.TextWidth(DBGrid1.Columns(lngCol).Caption))
>          If lngCaptionWidth > lngColumnWidth Then
>              lngColumnWidth = lngCaptionWidth
>          End If
>          DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
>      Next lngCol


> > Thanks for the tips. I tried it out and I couldnt figured out where the
> > 'objField' is defined, in method #1. Some explanations would be
> > appreciated. Thanks.


> > > These's no magic technique to get it to automatically set itself to
> > > the width of its widest entry (if only!) so you have to work it out
> > > for yourself. I'll assume you're using ADO.

> > > 1. Quick and Dirty
> > > Each Field has a DefinedSize property which returns the maximum number
> > > of characters any value in that column could have. You can use that
> > > and the form's TextWidth method to get a width for the column:

> > >     Dim lngCol          As Long
> > >     Dim lngColumnWidth  As Long
> > >     Dim lngCaptionWidth As Long

> > >     For lngCol = 0 To rst.Fields.Count
> > >         lngColumnWidth = Me.TextWidth(String$(objField.DefinedSize,
> > > "M"))
> > >         lngCaptionWidth =
> > > Me.TextWidth(DBGrid1.Columns(lngCol).Caption))
> > >         If lngCaptionWidth > lngColumnWidth Then
> > >             lngColumnWidth = lngCaptionWidth
> > >         End If
> > >         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
> > >     Next lngCol

> > > 2. Better-Looking But More Work
> > > Loop through all the rows in the recordset and use the ActualSize
> > > property to get the string lengths of all the entries in that field.
> > > Keep a note of the longest length and then use the TextWidth method to
> > > convert it to a column width.

> > >     Dim lngCol          As Long
> > >     Dim lngColumnWidth  As Long
> > >     Dim objField        As ADODB.Field

> > >     For lngCol = 0 To rst.Fields.Count
> > >         lngColumnWidth = Len(DBGrid1.Columns(lngCol).Caption))
> > >         Set objField = rst.Fields(lngCol)
> > >         rst.MoveFirst
> > >         Do Until rst.EOF
> > >             If objField.ActualSize > lngColumnWidth Then
> > >                 lngColumnWidth = objField.ActualSize
> > >             End If
> > >             rst.MoveNext
> > >         Loop
> > >         lngColumnWidth = Me.TextWidth(String$(lngColumnWidth, "M"))
> > >         DBGrid1.Columns(lngCol).Width = lngColumnWidth * 1.2
> > >     Next lngCol

> > > This is quick enough with up to about 100 rows but you can refine it
> > > by avoiding the loop for certain columns, depending on the data type.
> > > For example a date column will be as wide as "88/88/8888" (or "88 MMM
> > > 8888" or whatever), unless the column heading is wider in which case
> > > you'd use that. If you know the column is a number no bigger that
> > > "1,000,000.00" you could use that.

> > > NB: The Me.TextWidth method needs the form and the grid to have the
> > > same font.



Sat, 02 Oct 2004 10:12:21 GMT  
 How to resize the column width in DBGrid?
It would be easier if I could see what you're doing. Can I see your code, please?


Sun, 03 Oct 2004 20:53:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Make dbgrid columns width follow form width ???

2. How to get control if you resize the width of a Column in a ListView

3. URGENT : Width datagrid columns problem resize

4. Auto resizing column width for grid and listview...

5. MSFlexGrid column width resize event?

6. Auto resizing column width for grid and listview...

7. Auto resizing column width for grid and listview...

8. Auto resizing column width for grid and listview...

9. DBGrid control and column widths

10. HELP :Setting Column width in a DBGrid ?

11. ****Column Width of DBGrid****

12. VB4 - DBGrid columns width.

 

 
Powered by phpBB® Forum Software