Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting column width of a database grid. 1

Status
Not open for further replies.

djmc

Programmer
Jun 12, 2002
179
CA
Im using a db grid for displaying my ms access table. I want to know how I can set the column width individually so that all the values are viewable. I know you can one of properties in the dbgrid object but that only sets the width for ALL columns to be the same. I want to set custom widths for each field. Thanks.
 

DBGrid1.Columns(1).Width =xxx *******************************************************
[sub]General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
I have the same problem as Djmc's. I tried the code given by CCLINT, but it does not work. Could someone tell me how to set the column width individually so that all the values are viewable. Where I must put these code? I put the above code into one command button named Show Table, is it correct?

Thanks Chuan Wang
Royal Institute of Technology
Stockholm, Sweden
Homepage:
 
Please try form_activate event to put CCLINT's code.(Assuming that you are using the DBGrid in Bound mode, and the properties of the Data control have already been set at design time.)
 
Thanks for your reply.

I tried, but when run it, it does not work. There is one prompt like this

------
Complie error:
Method or data member not found
------

and in my code I used the following

----
MSFlexGrid1.Columns(1).Width = 1000
----

Because I used MSFlexGrid not DBGrid. Another thing that I do not understand the number in Columns(1), does it mean that it is the first column that you want to set the width.

Thank you very much for any help
Chuan Wang
Royal Institute of Technology
Stockholm, Sweden
Homepage:
 

Sure it works. You did say DB Grid.

If I have 10 columns in my grid and want to change the width of the second column, then:

DBGrid1.Columns(1).Width = 1000

will change it accordingly.

If you want to change the column width to the width of the widest text in the Visible rows, then you need to loop through the visible rows and find the widest text:

So now I am doing your work!

Private Sub SetGridColWidth(iCol As Integer)
Dim iWidthTest As Integer
Dim iTextWidth As Integer
Dim CurrBookmark As String

With DBGrid1
CurrBookmark = .RowBookmark(0)
For I = 0 To .VisibleRows - 1
.Row = I
iWidthTest = Me.TextWidth(.Columns(iCol ).Text)
If iWidthTest > iTextWidth Then iTextWidth = iWidthTest
Next I

.Columns(iCol ).Width = iTextWidth * 1.1
.Bookmark = CurrBookmark
End With
End Sub

If you want to adjust the rows for ALL records, then you will need to use the recordcount instead of the grid VisibleRow property

And no, I am not going to comment this Code. *******************************************************
[sub]General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

chuanwang: We are talking here about the DBGrid, as the questioner has asked, and not the Flex Grid.
*******************************************************
[sub]General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
chuanwang: You can try this in form_activate event:

MSFlexGrid1.ColWidth(1) = 500
MSFlexGrid1.ColWidth(2) = 3000.

This time, "1" means first column of grid, "2" means second column, and so on. (The "0"th column is supposed to be a Fixed Column.)

 

er, djmc, did you get it to work for the DBGrid? Please note that two different grids are being discussed here.... *******************************************************
[sub]General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
I used the .Columns(index).Width technique and placed the code into the Form_Activate function of the form and it set the column width correctly.

I tried using your SetGridColWidth function and modified it by setting a loop inside so that it would set the width for ALL my columns but it doesnt seem to work.
 
>>I used the .Columns(index).Width technique and placed the code into the Form_Activate function of the form and it set the column width correctly<<

I didn't suggest putting it in the Form_Activate. Someone else did. This is not a good place for code like this.

The code works. To see this, add a command button to the form and in the Button's Click event, call the proceedure that I gave above, passing the column you want to set, or, do the loop you mentioned and loop through the columns, setting each.
You will find, if done correctly, that it works. I would then use the code after the code where the grid is being set up. Maybe in the Form_Load event, at the end of the proceedure. Call DoEvents just prior.

*******************************************************
[sub]General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top