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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FORMATTING A VB6 DATAGRID 1

Status
Not open for further replies.

Hiccup

Programmer
Jan 15, 2003
266
US
I'm new with working with the DataGrids in VB6. I've created the Adodc and connected the DataControl DataGrid to it and retrieved the fields. How do I format the DataGrid such as center the headings at the top of the columns?

Thanks in advance.

Ed "Hiccup" Hicks
ed.hicks@wcg.com
 
Dude, there is some kind of glitch in VB 6. With the .alignment property for a datagrid, you can align the header to the left or right, but not to the center. You may be able to develop a little algorithm that takes the width of the colum subtracts the length of the string, then divide the difference by 2 and just add that many spaces to the front of the header text. I haven't actually tried it, but in theory it should work.

Good luck,
Kevin
 
You can center the headings, but the alignment property will also center the data in the column:

DataGrid1.Columns(0).Alignment=dbgCenter
 
Datagrid1.Columns(0).Caption = "ColumnTitle"
 
Thanks, scsii,

I added the following code you suggested to my DataGrid, but it didn't center the column headings.

Private Sub DataGrid1_Load()
DataGrid1.Columns(0).Caption = "ColumnTitle"
End Sub

Where you have "ColumnTitle" in your code do you mean the actual field name for the column such as "LastName" and should I have separate lines of this code for each Field Heading? Or should the "ColumnTitle" be entered in the code verbatim"

Also, should this "Private Sub" be a Load(), Click() or what?

Thanks in advance!

 
I think maybe scsii has missed the point - the suggestion will set the content of the column header to "ColumnTitle" (so you would choose a value that means something to the user to decribe the column) but it will have no effect on the alignment.
 
Bummer! Thanks anyway for your help, guys! I'll just leave everything flush left.
 
Sorry Hiccup for my misunderstanding

you just have to right click on the dtagrid itself / properties and select the layout tabs. there you will be able to set alignment of every column you want and all other formatting

hope it'll be helpful

scsii

 
Thanks, scsii, I'm aware of that DataGrid property window. Unfortunately, centering the headings also centers the data in the columns. I think it's just a flaw in VB not allowing one to center only the headings and not the column data.

Thanks anyway!
 
It's not really a flaw, as in the sense of a mistake.
IMO, it's a limitiation...deliberatly made.

But, in anycase, this is one of those limitations which you can get around with some code though.
Try the below code.
You may have to do some experimenting with it:

Sub CenterColHeaders()
Dim curWhiteSpace As Currency
Dim col As MSDataGridLib.Column
Dim FormFont As StdFont
Set FormFont = Me.Font

'DataGrid1.HeadFont.Bold = True
Set Me.Font = DataGrid1.HeadFont

For Each col In DataGrid1.Columns
col.Caption = col.DataField
col.Alignment = dbgLeft

curWhiteSpace = (col.Width - Me.TextWidth(col.Caption))

curWhiteSpace = Int((curWhiteSpace / Me.TextWidth(" ")) / 2)
If curWhiteSpace > 0 Then
col.Caption = Space(curWhiteSpace) & col.Caption & Space(curWhiteSpace + 2)
Else
col.Caption = Space(1) & col.Caption & Space(2)
End If
col.Width = Me.TextWidth(col.Caption)
Next col
Set Me.Font = FormFont
Set FormFont = Nothing
End Sub
 
er, make sure the Form1.ScaleMode is first set correctly
 
CCLINT, thanks again. I played with your code suggestion and got the Headings to center separately from the Column data.

You never cease to amaze me where you come with this stuff!

You get my vote!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top