Right alignment on Winforms Data Grid cuts off text on right.

E

Eric K

I am displaying currency data in a DataGrid column. The data is
right-aligned within the header and the entire column via the
alignment property. However, all of the text both in the header row
and the content rows is being slightly trimmed on the right-hand side
of the column no matter how wide the column is set. For instance the
text "Total" is being displayed in the header row as "Tota" etc...

Has anyone else encountered this error or have any ideas on how to
correct it?

Thanks
 
K

Ken Tucker [MVP]

Hi,

Two solutions. One put a period on the end of the header text so
that getts cropped off. Second is to create a datagridtextboxcolumn which
allows you to align you data and header differently. The example below adds
a dataalignment property. The alignment property sets the header alignment.
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

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
 
E

Eric K

Thank you for the help, but it doesn't actually address my main problem.
I made it a little unclear in my first entry. The header text AND the
values in the content rows are all being trimmed off on the right-side
of the columns when I have the alignment set to right. I cannot add a
"." to those values to fix the way they are viewed as they are currency
values. I do not know if it is a bug with the DataGrid or what, but it
is very strange behavior. We are having problems with trimming on the
right-hand side of most of our right-aligned columns.
 

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