Link to home
Start Free TrialLog in
Avatar of LarryZ
LarryZ

asked on

Center header text if column is right-justified in DataGrid

Hello Experts.

I have a DataGrid in a Windows form and am successfully controling column width and column alignment in code using DataGridTableStyles and GridColumnStyles - no problem. Column 1 ( width = 90) contains numeric data, so I used

    dgts.GridColumnStyles(1).Alignment = HorizontalAlignment.Right

to make the data right-justifed. The data looks good in the column, but the 3-character Header Text doesn't look very good when it is also right-justified - it looks like it is crammed too far to the right. I would like to center-justify the Header Text, but still leave the column data right-justified.

I unsuccessfully tried using the PadRight method like this:

        Dim strCol1Header As String = "UPC"
        dgts.GridColumnStyles(1).HeaderText = strCol1Header.PadLeft(9)

Does anyone know a way to have different justification for the Header Text and column data in a Windows-form DataGrid?

 
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In the Grid Header Style, align it to Center
Like this,
<HeaderStyle Font-Bold="True" HorizontalAlign=Center  ForeColor="White"></HeaderStyle>

Regards
JP
Avatar of LarryZ
LarryZ

ASKER

jpinia, my question referred to DataGrids on Windows forms, not Web forms.  Your answer seems like HTML for Web page.
I think there is no option in Window forms for that
Avatar of LarryZ

ASKER

I figured out a sloppy workaround, but I don't like it much:

        dgts.GridColumnStyles(1).HeaderText = "UPC" & Chr(32) & Chr(32) & Chr(32) & Chr(32) & Chr(32) & Chr(32) & Chr(32) & Chr(32) & Chr(31)


This seems to pad the region to the right of the "UPC" heading with invisible characters.