Home All Groups Group Topic Archive Search About

Centering a datagrid's column headers

Author
28 Feb 2007 6:08 PM
Patrick Hill
How do you center the text in the column headers of a datagird?  Using vb
6.0 and the standard Datagrid

Author
28 Feb 2007 6:41 PM
Rick Rothstein (MVP - VB)
> How do you center the text in the column headers of a
> datagird?  Using vb 6.0 and the standard Datagrid

I don't do database programming, so I've never used the Datagrid control.
However, if you click on Custom in the Properties Box and click the Layout
tab, it looks like you can set the Align property for each column
individually. I'm guessing if you select 2-dbgCenter, it will center the
column header and its data. Give it a try, it might do what you want.

Rick
Author
28 Feb 2007 7:13 PM
Patrick Hill
That doesn't work... the grid is bound to a recordset.  When I treid your
method I kept getting index out of bounds errors.  I don't believe this is
the way to accompliish this task.

Show quoteHide quote
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in
message news:O1%23cJg2WHHA.1208@TK2MSFTNGP03.phx.gbl...
>> How do you center the text in the column headers of a
>> datagird?  Using vb 6.0 and the standard Datagrid
>
> I don't do database programming, so I've never used the Datagrid control.
> However, if you click on Custom in the Properties Box and click the Layout
> tab, it looks like you can set the Align property for each column
> individually. I'm guessing if you select 2-dbgCenter, it will center the
> column header and its data. Give it a try, it might do what you want.
>
> Rick
>
Author
28 Feb 2007 7:35 PM
Rick Rothstein (MVP - VB)
>>> How do you center the text in the column headers of a
>>> datagird?  Using vb 6.0 and the standard Datagrid
>>
>> I don't do database programming, so I've never used the Datagrid control.
>> However, if you click on Custom in the Properties Box and click the
>> Layout tab, it looks like you can set the Align property for each column
>> individually. I'm guessing if you select 2-dbgCenter, it will center the
>> column header and its data. Give it a try, it might do what you want.
>
> That doesn't work... the grid is bound to a recordset.  When I treid your
> method I kept getting index out of bounds errors.  I don't believe this is
> the way to accompliish this task.

Okay, try it in code this way...

    DataGrid1.Columns(N).Alignment = dbgCenter

where N is the column number (substitute your actual control name for the
DataGrid1 name I used).

Rick
Author
28 Feb 2007 8:47 PM
Patrick Hill
I tried that and that just centers the data in the cell not the column
headers :(

Show quoteHide quote
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in
message news:OgDGZ%232WHHA.4872@TK2MSFTNGP03.phx.gbl...
>>>> How do you center the text in the column headers of a
>>>> datagird?  Using vb 6.0 and the standard Datagrid
>>>
>>> I don't do database programming, so I've never used the Datagrid
>>> control. However, if you click on Custom in the Properties Box and click
>>> the Layout tab, it looks like you can set the Align property for each
>>> column individually. I'm guessing if you select 2-dbgCenter, it will
>>> center the column header and its data. Give it a try, it might do what
>>> you want.
>>
>> That doesn't work... the grid is bound to a recordset.  When I treid your
>> method I kept getting index out of bounds errors.  I don't believe this
>> is the way to accompliish this task.
>
> Okay, try it in code this way...
>
>    DataGrid1.Columns(N).Alignment = dbgCenter
>
> where N is the column number (substitute your actual control name for the
> DataGrid1 name I used).
>
> Rick
>
>
Author
28 Feb 2007 9:17 PM
Rick Rothstein (MVP - VB)
Show quote Hide quote
>>>>> How do you center the text in the column headers of a
>>>>> datagird?  Using vb 6.0 and the standard Datagrid
>>>>
>>>> I don't do database programming, so I've never used the Datagrid
>>>> control. However, if you click on Custom in the Properties Box and
>>>> click the Layout tab, it looks like you can set the Align property for
>>>> each column individually. I'm guessing if you select 2-dbgCenter, it
>>>> will center the column header and its data. Give it a try, it might do
>>>> what you want.
>>>
>>> That doesn't work... the grid is bound to a recordset.  When I treid
>>> your method I kept getting index out of bounds errors.  I don't believe
>>> this is the way to accompliish this task.
>>
>> Okay, try it in code this way...
>>
>>    DataGrid1.Columns(N).Alignment = dbgCenter
>>
>> where N is the column number (substitute your actual control name for the
>> DataGrid1 name I used).
>
>I tried that and that just centers the data in the cell not the column
>headers :(

This is probably a completely wrong way to do it, but I tried this without
connecting to any database and it seemed to work...

Dim W As Long
Dim SpaceWidth As Long
Dim NumOfSpaces As Long
Dim ColumnHeaderText As String
ColumnHeaderText = "Hello"
With DataGrid1
  SpaceWidth = Me.TextWidth(" ")
  W = .Columns(1).Width
  NumOfSpaces = (W - Me.TextWidth(ColumnHeaderText)) \ SpaceWidth \ 2
  .Columns(1).Caption = Space$(NumOfSpaces) & ColumnHeaderText
End With

Rick