Hi,
I do not see where you add the columns to the tablestyle. Add
all the columns to the tablestyle before you add it to the datagrid.
When
you add a tablestyle to a grid set the tablestyle's readonly property
instead of the datagrids.
Ken
-------------
'using a read-only datagrid to show a summary
'code not commented out gives the Microsoft-standard smashed-in right
alignment
'code that IS commented out are lines used to call your class
'the connection, fill, stored procedure, etc. have worked fine for 2+
years,
display is only issue
'your class is shown at the bottom
Private Sub ShowSummaryHistory()
DataGrid1.DataSource = Nothing
dtHistory.Clear()
ds.Clear()
Dim strSQLServer As New SqlConnection(strConnString)
Dim cmd As New SqlCommand("GetJobSummaryHistory", strSQLServer)
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = strSQLServer
strSQLServer.Open()
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "dtHistory")
dtHistory = ds.Tables("dtHistory")
DataGrid1.DataSource = dtHistory
AdjustTableStyle()
End Sub
Private Sub AdjustTableStyle()
Dim tblStyle As New DataGridTableStyle
tblStyle.MappingName = "dtHistory"
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(tblStyle)
'Dim tbcSaleDate As New HeaderAndDataAlignColumn
Dim tbcSaleDate As DataGridTextBoxColumn =
CType(tblStyle.GridColumnStyles("SaleDate"), DataGridTextBoxColumn)
tbcSaleDate.MappingName = "SaleDate"
'this was not used with your class
tbcSaleDate.Alignment = HorizontalAlignment.Right
'this however, was
'tbcSaleDate.DataAlignment = HorizontalAlignment.Right
tbcSaleDate.HeaderText = "Sold"
tbcSaleDate.Width = 65
tbcSaleDate.NullText = ""
.... adding more columns
DataGrid1.CaptionText = "Job Summaries"
DataGrid1.ReadOnly = True
End Sub
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 Tucker said:
Hi,
If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code
Ken
---------------
Any known fixes for the wacky right-alignment bug in the WinForms
datagrid
(VS2003)?
I've tried Ken's workaround
(
http://www.windowsformsdatagridhelp.com/default.aspx?ID=4bfab32d-9cff-4f5c-ba95-49bb9074a8bc),
but I get no alignment at all when calling the class.
George Shephard's site, while imminently useful, does not have an anwer
for
this issue.
I posted on this topic about a year ago, and went in circles with
Microsoft
who wanted to pretend that there was some bug in my code instead of in
their
control. Enough time has passed and enough developers have posted on
this
issue, that I must say, no, I am not interested in wanting to "learn
more"
about 1 million MSDN webpages that are non-specific to this issue.