Datagrid Header Text Alignment problem?

J

John Smith

Hi all;

Putting "Due" into the column header of a datagrid.
Font is a proportional fort.

When the alignment is left, there is some space between the column separator
bar and the D in Due. When the alignment is center, Due is centered in the
header box. When the alignment is right, the e in Due is partially covered
by the column separator bar. Adding space after Due (i.e.. "Due ") does
not work. HeaderText seems to be trimmed before printing. Any suggestions on
how to position Due to the right but a few spaces away from the separator
bar?

Thanks.
 
K

Ken Tucker [MVP]

Hi,

Here is a columnstyle that I wrote that allows you to align the data
and column header differently. Set the alignment for the header and
dataalignment for the data. Hope this helps.

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 = Me.GetColumnValueAtRow([source], rowNum).ToString()

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

---------------------------

Hi all;

Putting "Due" into the column header of a datagrid.
Font is a proportional fort.

When the alignment is left, there is some space between the column separator
bar and the D in Due. When the alignment is center, Due is centered in the
header box. When the alignment is right, the e in Due is partially covered
by the column separator bar. Adding space after Due (i.e.. "Due ") does
not work. HeaderText seems to be trimmed before printing. Any suggestions on
how to position Due to the right but a few spaces away from the separator
bar?

Thanks.
 

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