?
Solved

Problem formating data grids programatically

Posted on 2006-04-08
1
Medium Priority
?
558 Views
Last Modified: 2008-02-01

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

0
Comment
Question by:laschwabe
1 Comment
 
LVL 2

Accepted Solution

by:
manojkumarps earned 2000 total points
ID: 16424058
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
Loops Section Overview
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question