DataGridView Column Alignment

  • Thread starter Thread starter Samuel Shulman
  • Start date Start date
Hi,

Change the alignment of the default cell style for the column.

Dim strConn As String
Dim da As SqlDataAdapter
Dim conn As SqlConnection

strConn = "Server = .;Database = AdventureWorks; Integrated Security
= SSPI;"
conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select * from HumanResources.Employee",
conn)

da.Fill(ds, "Employee")
DataGridView1.DataSource = ds.Tables("Employee")
DataGridView1.Columns("ContactID").DefaultCellStyle.Alignment =
DataGridViewContentAlignment.MiddleRight


Ken
 
In my solution, I have included a helper class to encapsulate some of these
standard settings. In that class, I have a function which gets a standardized
(for my application) currency formatter as follows:

Public Function DGCurrencyStyle() As DataGridViewCellStyle
Dim Style As New DataGridViewCellStyle
Style.Alignment = DataGridViewContentAlignment.MiddleRight
Style.Format = "c2"
Return Style
End Function

To use it, I modify the .Designer file and set the DataGridViewTextBoxColumn1.DefaultCellStyle=MyHelper.DGCurrencyStyle.
This way, if I want to change the style throughout my system, I just change
it in the helper and the change is reflected throughout the system.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
 
Thank you,

The actual column header remains on the left and in ListView control it
moves to the right like the content of the list

I am actually not sure what would be the correct way but is there a way of
changing that as well?

Samuel
 
Thank you,

The actual column header remains on the left and in ListView control it
moves to the right like the content of the list

I am actually not sure what would be the correct way but is there a way of
changing that as well?

Samuel


Me.DataGridView.Columns.Item(Index).HeaderCell.Style.Alignment = _
DataGridViewContentAlignment.MiddleCenter

Where Index can be the column index value or the column name string


Gene
 
Back
Top