Problem formating data grids programatically


Hi!


I am trying to format data grid columns programatically with vb.net 2003 code as follow:


Public Class DataGridFormattableTextBoxColumn
    Inherits DataGridTextBoxColumn
    Private _col As Integer
    'Fields
    'Constructors
    'Events
    'Methods
    Public Sub New(ByVal col As Integer)
        MyBase.New()
        _col = col

    End Sub
    Public Event SetCellFormat As FormatCellEventHandler
    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
        Dim drawFormat As New StringFormat
        Dim e As DataGridFormatCellEventArgs
        Dim s As String
        e = New DataGridFormatCellEventArgs(rowNum, Me._col, Me.DataGridTableStyle.DataGrid.Font, backBrush, foreBrush)
        RaiseEvent SetCellFormat(Me, e)
        If e.UseBaseClassDrawing Then
            MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
        Else
            g.FillRectangle(e.BackBrush, bounds)
            If IsNumeric(Me.GetColumnValueAtRow(source, rowNum)) Then
                drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft
                ' g.DrawString(Me.GetColumnValueAtRow(source, rowNum).ToString, e.TextFont, e.ForeBrush, bounds.X + bounds.Width, bounds.Y, drawFormat)
                's = Format(Me.GetColumnValueAtRow(source, rowNum), "#,###,##0.00")
                's.Format("#,###,##0.00")
                's = CStr(Me.GetColumnValueAtRow(source, rowNum))

                If (Me.GetColumnValueAtRow(source, rowNum) <> 0) Then
                    g.DrawString(Me.GetColumnValueAtRow(source, rowNum), e.TextFont, e.ForeBrush, bounds.X + bounds.Width, bounds.Y, drawFormat)
                Else
                    g.DrawString("", e.TextFont, e.ForeBrush, bounds.X + bounds.Width, bounds.Y, drawFormat)
                End If
            Else
                    g.DrawString(Me.GetColumnValueAtRow(source, rowNum).ToString, e.TextFont, e.ForeBrush, bounds.X, bounds.Y)
                End If
            End If
        If (e.TextFont Is Me.DataGridTableStyle.DataGrid.Font) = False Then
            e.TextFont.Dispose()
        End If

    End Sub

    Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)

        'comment to make cells unable to become editable
        MyBase.Edit(source, rowNum, bounds, [ReadOnly], instantText, cellIsVisible)

    End Sub
End Class

...

Public Class DataGridFormatCellEventArgs
    Inherits EventArgs
    Private _column As Integer
    Private _row As Integer
    Private _font As Font
    Private _backBrush As Brush
    Private _foreBrush As Brush
    Private _useBaseClassDrawing As Boolean
    'Fields
    'Constructors
    'Methods
    Public Sub New(ByVal row As Integer, ByVal col As Integer, ByVal font1 As Font, ByVal backBrush As Brush, ByVal foreBrush As Brush)
        MyBase.New()
        _row = row
        _column = col
        _font = font1
        _backBrush = backBrush
        _foreBrush = foreBrush
        _useBaseClassDrawing = False

    End Sub
    Public Property Column() As Integer
        Get

            Return _column

        End Get
        Set(ByVal Value As Integer)

            _column = Value

        End Set
    End Property
    Public Property Row() As Integer
        Get

            Return _row

        End Get
        Set(ByVal Value As Integer)

            _row = Value

        End Set
    End Property
    Public Property TextFont() As Font
        Get

            Return _font

        End Get
        Set(ByVal Value As Font)

            _font = Value

        End Set
    End Property
    Public Property BackBrush() As Brush
        Get

            Return _backBrush

        End Get
        Set(ByVal Value As Brush)

            _backBrush = Value

        End Set
    End Property
    Public Property ForeBrush() As Brush
        Get

            Return _foreBrush

        End Get
        Set(ByVal Value As Brush)

            _foreBrush = Value

        End Set
    End Property
    Public Property UseBaseClassDrawing() As Boolean
        Get

            Return _useBaseClassDrawing

        End Get
        Set(ByVal Value As Boolean)

            _useBaseClassDrawing = Value

        End Set
    End Property
End Class

...

AddCellFormattingColumnStyles(Me.grdColorcodes, New FormatCellEventHandler(AddressOf FormatGridCells))
....

Private Sub AddCellFormattingColumnStyles(ByVal grid As DataGrid, ByVal handler As FormatCellEventHandler)

Dim ts As DataGridTableStyle
ts = New DataGridTableStyle
Dim dt As DataView
dt = CType(grid.DataSource, DataView)
ts.MappingName = "codetable"
...

Dim ts As DataGridTableStyle
ts = New DataGridTableStyle
Dim dt As DataView
dt = CType(grid.DataSource, DataView)
ts.MappingName = "tcategor"

Dim cs As DataGridFormattableTextBoxColumn
cs = New DataGridFormattableTextBoxColumn(0)
cs.MappingName = "cno"
cs.HeaderText = "Seq"
cs.Width = 50

cs.Alignment = System.Windows.Forms.HorizontalAlignment.Center
cs.Format = "0000"

cs.ReadOnly = True
AddHandler cs.SetCellFormat, handler
ts.GridColumnStyles.Add(cs)

grid.TableStyles.Clear()
grid.TableStyles.Add(ts)

...

The data grid is populated at run time but the statements in Private Sub AddCellFormattingColumnStyles to set 'cs.Alignment' and 'cs.Format' do not work. The data is not centered and formated. If the statement cs.Format is changed to = "#,###,##0.00" a number such as 123456.78 is not displayed as 123,456.78 but as 123456.78.

What am I doing wrong?

Thanks in advance

laschwabeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

manojkumarpsCommented:
Hello laschwabe,
      You are doing everything right but for a very small thing.
Just add this line before dispose.

       MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
   
This should work for you.

If you need more help reply with requirements.

Regards,
Manoj
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.