DataGrid Col Header Alignment

D

Doug Bell

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
 
D

Doug Bell

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 ")
 
K

Ken Tucker [MVP]

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
 
D

Doug Bell

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 said:
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 said:
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 ")




http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc
 
K

Ken Tucker [MVP]

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 said:
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 said:
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 said:
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 ")



Hi,


http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc

Ken
---------------
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
 
D

Doug Bell

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 said:
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 said:
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 said:
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
---------------------
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 ")



Hi,
http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc
Ken
---------------
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top