Solved

DBGrid Column Width

Posted on 1998-04-28
8
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
A macro to Count the number of rows across all worksheets 3 151
clicking a shape in a frame array vb6 3 48
Advice in Xamarin 21 109
Set WorkSheet  not Working 9 52
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

730 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