Home All Groups Group Topic Archive Search About

DataGridView Column Alignment

Author
11 Aug 2006 10:27 AM
Samuel Shulman
Hi

For numeric columns I want to align it to the right

How can I do that?

Samuel

Author
11 Aug 2006 11:19 AM
Ken Tucker [MVP]
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
--------------------------------
Show quoteHide quote
"Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
news:%23j9DQDTvGHA.4160@TK2MSFTNGP06.phx.gbl...
> Hi
>
> For numeric columns I want to align it to the right
>
> How can I do that?
>
> Samuel
>
Author
11 Aug 2006 1:25 PM
Jim Wooley
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

Show quoteHide quote
> 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
> --------------------------------
> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
> news:%23j9DQDTvGHA.4160@TK2MSFTNGP06.phx.gbl...
>> Hi
>>
>> For numeric columns I want to align it to the right
>>
>> How can I do that?
>>
>> Samuel
>>
Author
11 Aug 2006 2:00 PM
Samuel Shulman
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

Show quoteHide quote
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uhVZLgTvGHA.1772@TK2MSFTNGP06.phx.gbl...
> 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
> --------------------------------
> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
> news:%23j9DQDTvGHA.4160@TK2MSFTNGP06.phx.gbl...
>> Hi
>>
>> For numeric columns I want to align it to the right
>>
>> How can I do that?
>>
>> Samuel
>>
>
>
Author
11 Aug 2006 11:06 PM
gene kelley
On Fri, 11 Aug 2006 15:00:05 +0100, "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote:

>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

Show quoteHide quote
>"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
>news:uhVZLgTvGHA.1772@TK2MSFTNGP06.phx.gbl...
>> 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
>> --------------------------------
>> "Samuel Shulman" <samuel.shul***@ntlworld.com> wrote in message
>> news:%23j9DQDTvGHA.4160@TK2MSFTNGP06.phx.gbl...
>>> Hi
>>>
>>> For numeric columns I want to align it to the right
>>>
>>> How can I do that?
>>>
>>> Samuel
>>>
>>
>>
>