emaduddeen
Programmer
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:
Thanks.
Truly,
Emad
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