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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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]" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hi,
>>
>>
> http://www.windowsformsdatagridhelp....5-49bb9074a8bc
>>
>> Ken
>> ---------------
>> "Doug Bell" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>> > 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
>> >
>> >
>>
>>
>
>