PC Review


Reply
Thread Tools Rate Thread

DataGrid Col Header Alignment

 
 
Doug Bell
Guest
Posts: n/a
 
      18th Aug 2005
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


 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      18th Aug 2005
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
>
>



 
Reply With Quote
 
Doug Bell
Guest
Posts: n/a
 
      18th Aug 2005
Thanks Ken

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

>
>



 
Reply With Quote
 
Doug Bell
Guest
Posts: n/a
 
      18th Aug 2005
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
> >
> >

>
>



 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      18th Aug 2005
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
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
Doug Bell
Guest
Posts: n/a
 
      18th Aug 2005
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]" <(E-Mail Removed)> wrote in message
news:%23wgw8$(E-Mail Removed)...
> 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
> >> >
> >> >
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      18th Aug 2005
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" <(E-Mail Removed)> wrote in message
news:%23Fi31K%(E-Mail Removed)...
> 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]" <(E-Mail Removed)> wrote in message
> news:%23wgw8$(E-Mail Removed)...
>> 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
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >

>>
>>

>
>



 
Reply With Quote
 
Doug Bell
Guest
Posts: n/a
 
      18th Aug 2005
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]" <(E-Mail Removed)> wrote in message
news:uR8rjq%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:%23Fi31K%(E-Mail Removed)...
> > 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]" <(E-Mail Removed)> wrote in message
> > news:%23wgw8$(E-Mail Removed)...
> >> 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
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Header Alignment Mrozu Microsoft VB .NET 1 17th Dec 2005 11:46 AM
Datagrid different alignment for Header and Rows robear@joshie.com.au Microsoft VB .NET 1 18th Oct 2005 02:57 AM
DataGrid Header and Text Alignment =?Utf-8?B?UGFua2FqQmFuZ2E=?= Microsoft Dot NET 2 8th Jul 2004 02:26 PM
DataGrid column header alignment cnu Microsoft C# .NET 0 23rd Sep 2003 03:36 AM
Re: DataGrid Header Alignment Vlad Microsoft VB .NET 0 8th Jul 2003 02:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:57 AM.