Format Database Numeric Field to Appear as Currency in DataGrid

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


Who is Participating?
wtconwayConnect With a Mentor Commented:
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
hedbustaAuthor Commented:
Thanks!, That it!

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.

All Courses

From novice to tech pro — start learning today.