Solved

DBGrid Column Width

Posted on 1998-04-28
8
420 Views
Last Modified: 2010-05-18
How do you set the Columns Collection width property to display 50 charecters of data (or any number of charecters)?  I told a look at the width property of one of my columns and it is in the form of "xxxx.xxx".  What does this mean? I do not want to upgrade to TDBGrid.
0
Comment
Question by:baileys
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:stevesm
ID: 1433318
Baileys,

Try this code on a 4 column grid with 2 rows on the load.
Alter the code below to suit your needs

Private Sub Form_Load()

'Configure the grid by setting the columns,
'captions,widths and alignments
    grdYourGrid.Row = 0
'Set the properties for the first column
    grdYourGrid.Col = 0
    grdYourGrid.Text = "Column 1"
    grdYourGrid.ColWidth(0) = 600
    grdYourGrid.FixedAlignment(0) = vbCenter
'Set the properties for the second column
    grdYourGrid.Col = 1
    grdYourGrid.Text = "Column 2"
    grdYourGrid.ColWidth(1) = 1900
    grdYourGrid.ColAlignment(1) = vbLeftJustify
'Set the properties for the third column
    grdYourGrid.Col = 2
    grdYourGrid.Text = "Column 3"
    grdYourGrid.ColWidth(2) = 800
    grdYourGrid.ColAlignment(2) = vbCenter
'Set the properties for the fourth column
    grdYourGrid.Col = 3
    grdYourGrid.Text = "Column 4"
    grdYourGrid.ColWidth(3) = 1600
    grdYourGrid.ColAlignment(3) = vbLeftJustify
End Sub


Let me know if you need any more info by rejecting my answer and commenting back.

Good luck.....Steve

0
 

Author Comment

by:baileys
ID: 1433319
Got an error.  COLWIDTH was not a method of DBGrid.

                Sheila
0
 

Expert Comment

by:mattyboy
ID: 1433320
Just a side note....

Setting the width of a column sets it in pixels, not in characters.  So, to find out how wide 50 characters is:

Create a Label on your Form, and make it invisible.  Set it's AutoSize property to True.  Change the Caption of the Label to the text you are going to display in your column.  Then, set the width of the column to the width of the Label.

mattyboy
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Expert Comment

by:mattyboy
ID: 1433321
ooops
twips, not pixels!

mattyboy
0
 
LVL 1

Expert Comment

by:stevesm
ID: 1433322
Sheila,

I was wrong the code is for a flex grid not a dbgrid.

The property setting is in the property pages for the dbgrid on the tab marked layout where it has a label marked "width". You right click on the control for the pop up menu.

Let me know if this works....Steve
0
 
LVL 6

Accepted Solution

by:
clifABB earned 100 total points
ID: 1433323
If you can make the form have the same font as the grid, use this code:

'To make column 1 50 characters wide
DBGrid1.Columns(1).Width = Form1.TextWidth(String(50,"X"))

Remember that different fonts have different widths and there are some fonts where each character has a different with.  This font you're reading is a good example, the "I" is much narrower than a "W".  Using an "X" in this case is a good median value.
0
 

Author Comment

by:baileys
ID: 1433324
That solved the problem perfectly. I have been trying to solve this problem off and on for the last 3 weeks.  Thanks!!

                        Sheila
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1433325
You're quite welcome.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add text to end of existing text in file 16 71
Need VBS version of Excel date conversion 4 71
Saving history changes to sub form 4 44
Formula problem with Excel attachment 6 28
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question