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

How do you right-align a DataGrid header using code? 1

Status
Not open for further replies.

emaduddeen

Programmer
Mar 22, 2007
184
US
Hi Everyone,

Can you tell me how to right-align the text in a DataGrid using code?

The data in the grid displays OK. I just want to right align the headers.

Here is how the grid is populated:

Code:
Public Class frmComplaints
    Private objDatabaseStuff As New clsDbUtil

    Public intComplaintID As Integer ' I can be seen by any form in this project.

    Private Sub LoadGridWithData()
        grdComplaints.DataSource = _
        objDatabaseStuff.GetDataView("SELECT Complaints.CompaintID, Status.StatusTitle, " & _
                                            "Complaints.Description, Complaints.Location, " & _
                                            "Complaints.OpenDate " & _
                                       "FROM Complaints " & _
                                 "INNER JOIN Status ON Complaints.StatusID = Status.StatusID " & _
                                   "ORDER BY Complaints.OpenDate DESC")
    End Sub

    Private Sub FormatGrid(ByVal TheGridView As System.Windows.Forms.DataGridView)
        ' Define style objects.
        '----------------------
        Dim objRightAlignStyle As New DataGridViewCellStyle
        Dim objGreenBarEffectStyle As New DataGridViewCellStyle

        ' Set properties to the style objects.
        '-------------------------------------
        objRightAlignStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        objGreenBarEffectStyle.BackColor = Color.Goldenrod

        ' Apply the styles to the DataGrid.
        '----------------------------------
        With TheGridView
            .AlternatingRowsDefaultCellStyle = objGreenBarEffectStyle ' Apply the green-bar effect.

            .Columns("CompaintID").HeaderText = "ID"
            .Columns("CompaintID").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns("CompaintID").DefaultCellStyle = objRightAlignStyle
            .Columns("CompaintID").HeaderCell.Style = objRightAlignStyle

            .Columns("OpenDate").HeaderText = "Opened"
            .Columns("OpenDate").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns("OpenDate").DefaultCellStyle = objRightAlignStyle
            .Columns("OpenDate").HeaderCell.Style = objRightAlignStyle

            .Columns("StatusTitle").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns("Description").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns("Location").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        End With

    End Sub

    Private Sub LoadComplaints()
        LoadGridWithData()
        FormatGrid(grdComplaints)
    End Sub

    Private Sub frmComplaints_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadComplaints()
    End Sub
End Class

Thanks.

Truly,
Emad
 
You can try:

Code:
.Columns("ColName").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
 
Hi Bluejay07,

Just tried it, but it's still left-aligned.

Thanks for the reply.

Truly,
Emad
 
Code:
        Dim ndcs As DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
        ndcs.Alignment = DataGridViewContentAlignment.MiddleRight

        .Columns("ColName").DefaultCellStyle = ndcs

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Sorry you said headers. That does the cells. Here is the headers.
Code:
        Dim ndcs As DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle(TheGridView.ColumnHeadersDefaultCellStyle)
        ndcs.Alignment = DataGridViewContentAlignment.MiddleRight

        TheGridView.ColumnHeadersDefaultCellStyle = ndcs

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Sorwen,

I tried it but same result.

Code:
        ' Define style objects.
        '----------------------
        Dim objRightAlignStyle As New DataGridViewCellStyle
        Dim objGreenBarEffectStyle As New DataGridViewCellStyle
        Dim ndcs As DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
        ndcs.Alignment = DataGridViewContentAlignment.MiddleRight

        ' Apply the styles to the DataGrid.
        '----------------------------------
        .Columns("ComplaintID").HeaderText = "ID"
        .Columns("ComplaintID").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        .Columns("ComplaintID").DefaultCellStyle = ndcs
        .Columns("ComplaintID").HeaderCell.Style = objRightAlignStyle

Truly,
Emad
 
It looks like you didn't watch what I put. Like I explained in the second post if you follow the first like you have it there it is going to adjust the "ComplaintID" cells of the grid not the header. If you want to do all of the headers right then you need to use the second post before your .with or modify it to place inside. If you use both be sure to name each different like ndcs and ndcs2. Now if you only want to change one header or only select headers you need to use just the first part of the second post.

Code:
        ' Define style objects.
        '----------------------
        Dim objRightAlignStyle As New DataGridViewCellStyle
        Dim objGreenBarEffectStyle As New DataGridViewCellStyle
        Dim ndcs As DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle
        ndcs.Alignment = DataGridViewContentAlignment.MiddleRight
[red]        Dim ndcs2 As DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle(TheGridView.ColumnHeadersDefaultCellStyle)
        ndcs2.Alignment = DataGridViewContentAlignment.MiddleRight[/red]


        ' Apply the styles to the DataGrid.
        '----------------------------------
        .Columns("ComplaintID").HeaderText = "ID"
        .Columns("ComplaintID").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        .Columns("ComplaintID").DefaultCellStyle = ndcs
        .Columns("ComplaintID").HeaderCell.Style = [red]ndcs2[/red]
or you can call it objRightAlignStyle like you wanted, but either way you need a different one for cells than you use for headers. If you see you are missing the steps that actually tell objRightAlignStyle what it needs to do.

Let me explain this more in depth. The headers are not default System.Windows.Forms.[red]DataGridViewCellStyle[/red] that is all you are doing with "Dim objRightAlignStyle As New DataGridViewCellStyle". They are modified by default that is why I fist make them match whatever the current style of the grid is with "New System.Windows.Forms.DataGridViewCellStyle(TheGridView.ColumnHeadersDefaultCellStyle)". Two things wrong with just a default style is that the color is white and it is left aligned. So since we took care that all of the default settings to now match the current grid so we now have to change the left aligned to right with "ndcs2.Alignment = DataGridViewContentAlignment.MiddleRight". The DataGridViewCellStyle is now how you want it and is ready to use for a header cell style only.

That may have been over explaining it, but I'm not sure where you might be getting lost in the use of it.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi,

Thanks for the reply but I'm still stuck.

Thank you so much for taking the time to help me on this.

Here is the changed sub procedure with the new code highlighted.

Code:
    Private Sub FormatGrid(ByVal TheGridView As System.Windows.Forms.DataGridView)
        ' Define style objects.
        '----------------------
        Dim objRightAlignCellStyle As New DataGridViewCellStyle
        Dim objGreenBarEffectStyle As New DataGridViewCellStyle
        Dim objRightAlignHeaderStyle As New DataGridViewCellStyle(TheGridView.ColumnHeadersDefaultCellStyle)
[red]
        Dim ndcs2 As DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle(TheGridView.ColumnHeadersDefaultCellStyle)
[/red]

        ' Set properties to the style objects.
        '-------------------------------------
        objRightAlignCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight                objGreenBarEffectStyle.BackColor = Color.Goldenrod
[red]
        ndcs2.Alignment = DataGridViewContentAlignment.MiddleRight
[/red]

        ' Apply the styles to the DataGrid.
        '----------------------------------
        With TheGridView
            .AlternatingRowsDefaultCellStyle = objGreenBarEffectStyle ' Apply the green-bar effect.

            .Columns("ComplaintID").HeaderText = "ID"
            .Columns("ComplaintID").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
[red]
            .Columns("ComplaintID").HeaderCell.Style = ndcs2
[/red]
            .Columns("ComplaintID").DefaultCellStyle = objRightAlignCellStyle

            .Columns("OpenDate").HeaderText = "Opened"
            .Columns("OpenDate").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns("OpenDate").DefaultCellStyle = objRightAlignCellStyle

            .Columns("StatusTitle").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns("Description").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            .Columns("Location").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        End With

    End Sub
 
I have to assume it still isn't right justifying it since you don't say what the problem is. Assuming no error is popping up then I'm not 100% sure what the problem could be. You could trying changing how the Grid is passed.

Change
Code:
Private Sub FormatGrid(ByVal TheGridView As System.Windows.Forms.DataGridView)

To this
Code:
Private Sub FormatGrid(ByRef TheGridView As System.Windows.Forms.DataGridView)
I wouldn't think that would matter though as long as the object is part of the form it should be already changing the original reference.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi,

No errors are actually reported and the data shows up ok and the cells are right aligning ok. Only the header is not right aligning.

Using ByVal and ByRef also give the same results.

Truly,
Emad
 
I tried your code directly and it works for me. The only other thing I could say is due to the space it keeps for the filter symbol it could look like it is center justified rather than right justified. I don't think you can move that without messing with the paint event.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi,

Thanks for trying the code.

I see what you mean about the sorting symbol. I just clicked on the header and noticed that it does take up some real estate on the grid.

I added a width number and it did right justify almost to the right side.

Truly,
Emad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top