By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,526 Members | 1,895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,526 IT Pros & Developers. It's quick & easy.

Any other workarounds for the totally screwball right-alignment on datagrid?

P: n/a
Any known fixes for the wacky right-alignment bug in the WinForms datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code

Ken
---------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
Any known fixes for the wacky right-alignment bug in the WinForms datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.

Nov 21 '05 #2

P: n/a
'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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code

Ken
---------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
Any known fixes for the wacky right-alignment bug in the WinForms datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.

Nov 21 '05 #3

P: n/a
Just a note to keep this thread alive -- do you see anything wrong with the
code I wrote to implement your class Ken?

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code

Ken
---------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
Any known fixes for the wacky right-alignment bug in the WinForms datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.

Nov 21 '05 #4

P: n/a
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
-------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
'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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code

Ken
---------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
Any known fixes for the wacky right-alignment bug in the WinForms datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.


Nov 21 '05 #5

P: n/a
Thanks for the reply Ken.

As shown in the code, the columns were added to the tablestyle after I
created the tablestyle (see tbcSaleDate).

However, the columns were added to the tablestyle AFTER I added it to the
datagrid (I'm not aware of a technique to prevent an exception trying that
your way).

Changing the tablestyle to readonly instead of the datagrid to readonly
didn't fix the right-alignment appearance.

In sum, here are the steps I used in the previous code to create a new
tablestyle. I'm still totally baffled by this crashed-in right-alignment
issue.

1. Set the datagrid datasource (DataGrid1.DataSource = dtHistory)
2. Create a new tablestyle (Dim tblStyle As New DataGridTableStyle)
3. Set the tablestyle mapping name (either tblStyle.MappingName =
"dtHistory" or tblStyle.MappingName = dtHistory.TableName.ToString)
4. Set the tablestyle to readonly
5. Clear the default datagrid tablestyle (DataGrid1.TableStyles.Clear()
6. Add the new tablestyle to the datagrid
(DataGrid1.TableStyles.Add(tblStyle))
7. Create the new datacolumns (Dim tbcSaleDate As DataGridTextBoxColumn =
CType(tblStyle.GridColumnStyles("SaleDate"), DataGridTextBoxColumn))

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
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
-------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
'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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code

Ken
---------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
Any known fixes for the wacky right-alignment bug in the WinForms
datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.


Nov 21 '05 #6

P: n/a
Oooooook ... I think I've found something here. Instantiating the columns at
the same time as I'm attaching them to the table style may be creating this
issue. Hold your fire until I experiment a little more.

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
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
-------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
'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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code

Ken
---------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
Any known fixes for the wacky right-alignment bug in the WinForms
datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.


Nov 21 '05 #7

P: n/a
Revised the code to not instantiate at same time as I map the datacolumn to
the textboxcolumn. This did not fix the right-alignment issue (clearly an
undocumented "feature"). However, it did allow me to now use your code fix,
which aligns the data to the right while the column name stays on the left
or center (either is adequate for my purposes). Much thanks for the fix!

"Earl" <br******@newsgroups.nospam> wrote in message
news:er**************@TK2MSFTNGP15.phx.gbl...
Oooooook ... I think I've found something here. Instantiating the columns
at the same time as I'm attaching them to the table style may be creating
this issue. Hold your fire until I experiment a little more.

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
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
-------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
'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 [MVP]" <vb***@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi,

If the mappingname is wrong for the tablestyle the tablestyle
will not work. Post some code

Ken
---------------
"Earl" <br******@newsgroups.nospam> wrote in message
news:uj**************@TK2MSFTNGP15.phx.gbl...
Any known fixes for the wacky right-alignment bug in the WinForms
datagrid
(VS2003)?

I've tried Ken's workaround
(http://www.windowsformsdatagridhelp....-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.



Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.