• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 372
  • Last Modified:

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"
                    TableStyle.GridColumnStyles.Add(dgTextBoxCol)
                End If
            Next

             dgCompany.TableStyles.Add(TableStyle)

TIA,
Neil
0
hedbusta
Asked:
hedbusta
1 Solution
 
wtconwayCommented:
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.GridColumnStyles.Add(colStyle)
        Next
        tblStyle.DataGrid = p_Grid
        tblStyle.MappingName = p_tbl.TableName
        p_Grid.TableStyles.Add(tblStyle)
    End Sub
0
 
hedbustaAuthor Commented:
Thanks!, That it!

Cheers,
Neil
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now