Format Database Numeric Field to Appear as Currency in DataGrid

Posted on 2006-04-28
Last Modified: 2010-08-05
Hi Experts,
I have a datagrid.  I would like to have all of my numeric(123123 or 123123.00)  fields in my dataset appear as currency($123,123.00 or at least 123,123.00) in my datagrid.  It should be fairly dynamic.  The grid has over 170 columns and a majority of them are currency.   I would prefer not to have to label these columns individually.  I have created a loop as below, but I fear I may be using the gridtextboxcolumn incorrectly.

            dv = New DataView(MainDataSet.Tables("Company"))
            dgCompany.DataSource = dv
            dgCompany.AlternatingBackColor = System.Drawing.Color.LightCyan
            dgCompany.CaptionVisible = False

            Dim ColumnCursor As Integer
            Dim TableStyle As New DataGridTableStyle

            For ColumnCursor = 0 To (MainDataSet.Tables("Company").Columns.Count - 1)
                If MainDataSet.Tables("Company").Columns(ColumnCursor).DataType.ToString = "System.Decimal" Then
                    dgTextBoxCol = New DataGridTextBoxColumn
                    dgTextBoxCol.Format = "c"
                End If


Question by:hedbusta
    LVL 7

    Accepted Solution

    This simple method will format all your System.Decimal columns as currency as leave the others alone. Note however, that you have to add columnstyles for them in order for them to display. It's kinda like, if you customize one, you customize them all kinda thing. I think. ;)

        Private Sub FormatDataGrid(ByVal p_Grid As DataGrid, ByVal p_tbl As DataTable)
            Dim tblStyle As New DataGridTableStyle
            Dim col As DataColumn
            For Each col In p_tbl.Columns
                Dim colStyle As New DataGridTextBoxColumn
                colStyle.FormatInfo = Nothing
                colStyle.MappingName = col.ColumnName
                If col.DataType.ToString() = "System.Decimal" Then 'this is a currency column
                    colStyle.Format = "c"
                End If
            tblStyle.DataGrid = p_Grid
            tblStyle.MappingName = p_tbl.TableName
        End Sub

    Author Comment

    Thanks!, That it!


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now