468,540 Members | 2,194 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,540 developers. It's quick & easy.

DataGrid Col Header Alignment

Hi
Does anyone know (or point me where I can find) how to set the alignment of
a DataGrid Column Header different to the alignment of the column.

I am trying to show some Right aligned columns and the header looks wrong
squashed to the right. If I could even add a trailing space but it trims any
trailing spaces off.

Thanks

Doug
Nov 21 '05 #1
7 1746
Hi,

http://www.windowsformsdatagridhelp....5-49bb9074a8bc

Ken
---------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Hi
Does anyone know (or point me where I can find) how to set the alignment
of
a DataGrid Column Header different to the alignment of the column.

I am trying to show some Right aligned columns and the header looks wrong
squashed to the right. If I could even add a trailing space but it trims
any
trailing spaces off.

Thanks

Doug

Nov 21 '05 #2
Thanks Ken

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

http://www.windowsformsdatagridhelp....5-49bb9074a8bc
Ken
---------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Hi
Does anyone know (or point me where I can find) how to set the alignment
of
a DataGrid Column Header different to the alignment of the column.

I am trying to show some Right aligned columns and the header looks wrong squashed to the right. If I could even add a trailing space but it trims
any
trailing spaces off.

Thanks

Doug


Nov 21 '05 #3
Ken,
That works very well, leaving the header Left Aligned while setting the Data
to Right Aligned.

I merged it in with my Paint and Edit Overrides.

It does however ignore the number formatting now.
I had the Format property set to :
..Format = ("#,##0.000 ;(#,##0.000) ;0.000 ")

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

http://www.windowsformsdatagridhelp....5-49bb9074a8bc
Ken
---------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Hi
Does anyone know (or point me where I can find) how to set the alignment
of
a DataGrid Column Header different to the alignment of the column.

I am trying to show some Right aligned columns and the header looks wrong squashed to the right. If I could even add a trailing space but it trims
any
trailing spaces off.

Thanks

Doug


Nov 21 '05 #4
Hi,

Here is an updated version

Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Decimal Then
s = CDec(Me.GetColumnValueAtRow([source],
rowNum)).ToString(Me.Format)
Else
s = Me.GetColumnValueAtRow([source], rowNum).ToString
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class

Ken
---------------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
Ken,
That works very well, leaving the header Left Aligned while setting the
Data
to Right Aligned.

I merged it in with my Paint and Edit Overrides.

It does however ignore the number formatting now.
I had the Format property set to :
.Format = ("#,##0.000 ;(#,##0.000) ;0.000 ")

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

http://www.windowsformsdatagridhelp....5-49bb9074a8bc

Ken
---------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
> Hi
> Does anyone know (or point me where I can find) how to set the
> alignment
> of
> a DataGrid Column Header different to the alignment of the column.
>
> I am trying to show some Right aligned columns and the header looks wrong > squashed to the right. If I could even add a trailing space but it
> trims
> any
> trailing spaces off.
>
> Thanks
>
> Doug
>
>



Nov 21 '05 #5
Ken,
That still does not format the field.
In that particular case, the Data Type is a Double and your code tests for
Decimal.

So changing the code to:
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Double Then
Works but If I wanted to make the code generic for all data types would I
have to use a Select and then format for each different type or is there a
more elegant way to make it generic?

eg
Select Case TypeOf Me.GetColumnValueAtRow([source], rowNum)

Case Is Integer

Case Is Double

Case Is String
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

Here is an updated version

Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Decimal Then
s = CDec(Me.GetColumnValueAtRow([source],
rowNum)).ToString(Me.Format)
Else
s = Me.GetColumnValueAtRow([source], rowNum).ToString
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class

Ken
---------------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
Ken,
That works very well, leaving the header Left Aligned while setting the
Data
to Right Aligned.

I merged it in with my Paint and Edit Overrides.

It does however ignore the number formatting now.
I had the Format property set to :
.Format = ("#,##0.000 ;(#,##0.000) ;0.000 ")

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

http://www.windowsformsdatagridhelp....5-49bb9074a8bc

Ken
---------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
> Hi
> Does anyone know (or point me where I can find) how to set the
> alignment
> of
> a DataGrid Column Header different to the alignment of the column.
>
> I am trying to show some Right aligned columns and the header looks

wrong
> squashed to the right. If I could even add a trailing space but it
> trims
> any
> trailing spaces off.
>
> Thanks
>
> Doug
>
>



Nov 21 '05 #6
Hi,

I need to add that functionality to the class. I havent come up
with the best method and will post an update when I do.

Ken
-----------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Ken,
That still does not format the field.
In that particular case, the Data Type is a Double and your code tests for
Decimal.

So changing the code to:
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Double Then
Works but If I wanted to make the code generic for all data types would I
have to use a Select and then format for each different type or is there a
more elegant way to make it generic?

eg
Select Case TypeOf Me.GetColumnValueAtRow([source], rowNum)

Case Is Integer

Case Is Double

Case Is String
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

Here is an updated version

Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal

bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal

instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Decimal
Then
s = CDec(Me.GetColumnValueAtRow([source],
rowNum)).ToString(Me.Format)
Else
s = Me.GetColumnValueAtRow([source], rowNum).ToString
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class

Ken
---------------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
> Ken,
> That works very well, leaving the header Left Aligned while setting the
> Data
> to Right Aligned.
>
> I merged it in with my Paint and Edit Overrides.
>
> It does however ignore the number formatting now.
> I had the Format property set to :
> .Format = ("#,##0.000 ;(#,##0.000) ;0.000 ")
>
>
>
> "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
>> Hi,
>>
>>
> http://www.windowsformsdatagridhelp....5-49bb9074a8bc >>
>> Ken
>> ---------------
>> "Doug Bell" <Po*********@vodaphone.com.au> wrote in message
>> news:%2***************@TK2MSFTNGP14.phx.gbl...
>> > Hi
>> > Does anyone know (or point me where I can find) how to set the
>> > alignment
>> > of
>> > a DataGrid Column Header different to the alignment of the column.
>> >
>> > I am trying to show some Right aligned columns and the header looks
> wrong
>> > squashed to the right. If I could even add a trailing space but it
>> > trims
>> > any
>> > trailing spaces off.
>> >
>> > Thanks
>> >
>> > Doug
>> >
>> >
>>
>>
>
>



Nov 21 '05 #7
Ken,
Thanks for your help.
My Grid is working really well for this project.

I will keep an eye on your site.

Doug

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uR****************@TK2MSFTNGP14.phx.gbl...
Hi,

I need to add that functionality to the class. I havent come up with the best method and will post an update when I do.

Ken
-----------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
Ken,
That still does not format the field.
In that particular case, the Data Type is a Double and your code tests for
Decimal.

So changing the code to:
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Double Then
Works but If I wanted to make the code generic for all data types would I have to use a Select and then format for each different type or is there a more elegant way to make it generic?

eg
Select Case TypeOf Me.GetColumnValueAtRow([source], rowNum)

Case Is Integer

Case Is Double

Case Is String
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi,

Here is an updated version

Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal

bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal

instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String
If TypeOf Me.GetColumnValueAtRow([source], rowNum) Is Decimal
Then
s = CDec(Me.GetColumnValueAtRow([source],
rowNum)).ToString(Me.Format)
Else
s = Me.GetColumnValueAtRow([source], rowNum).ToString
End If
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class

Ken
---------------------
"Doug Bell" <Po*********@vodaphone.com.au> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
> Ken,
> That works very well, leaving the header Left Aligned while setting the > Data
> to Right Aligned.
>
> I merged it in with my Paint and Edit Overrides.
>
> It does however ignore the number formatting now.
> I had the Format property set to :
> .Format = ("#,##0.000 ;(#,##0.000) ;0.000 ")
>
>
>
> "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
> news:%2****************@tk2msftngp13.phx.gbl...
>> Hi,
>>
>>
>

http://www.windowsformsdatagridhelp....5-49bb9074a8bc
>>
>> Ken
>> ---------------
>> "Doug Bell" <Po*********@vodaphone.com.au> wrote in message
>> news:%2***************@TK2MSFTNGP14.phx.gbl...
>> > Hi
>> > Does anyone know (or point me where I can find) how to set the
>> > alignment
>> > of
>> > a DataGrid Column Header different to the alignment of the column.
>> >
>> > I am trying to show some Right aligned columns and the header looks > wrong
>> > squashed to the right. If I could even add a trailing space but it
>> > trims
>> > any
>> > trailing spaces off.
>> >
>> > Thanks
>> >
>> > Doug
>> >
>> >
>>
>>
>
>



Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jim Dougherty | last post: by
4 posts views Thread by Tim | last post: by
2 posts views Thread by Stan Sainte-Rose | last post: by
2 posts views Thread by pmcguire | last post: by
5 posts views Thread by EdB | last post: by
2 posts views Thread by John Smith | last post: by
1 post views Thread by Jose | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.