Question : Format Dynamically created datagrid's column to currency

I have a Gridview that is populated by a dataset, which is obtained from a SQL Server 2005 database.  The datasource is gathered dynamically, in that it could have varying amounts of columns.  
My question is this:
How do I format the Gridview column so that it shows as currency.  I know the format.string("{0:c}", somenumber), but that will not work for my scenario.  As the datasource is created in the code behind, I have no inItemBound selection in the codebehind window, otherwise I could format it accordingly.  
I can provide data if needed, but I think this is a mechanics issue, as the data is successfully gathered into the dataset properly.

Answer : Format Dynamically created datagrid's column to currency

check this out:
http://blogs.msdn.com/rahulso/archive/2006/02/28/Dynamic-GridView-Series-_2D00_-2.Formatting-Columns-dynamically.aspx

the trick is, it determines formatting based on the column type.  here is some simplified code i got working from it (just plop a gridview and a button on the form, then paste this in the code behind).  you might be able to get something out of this to point you in the right direction:

    Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' create datatable
        Dim theData As New DataTable
        theData.Columns.Add("Price", GetType(System.Decimal))
        theData.Columns.Add("Desc")

        ' put some data in it
        Dim aRow As DataRow = theData.NewRow
        aRow("Price") = 3.23
        aRow("Desc") = "something"
        theData.Rows.Add(aRow)

        aRow = theData.NewRow
        aRow("Price") = 32.83
        aRow("Desc") = "something else"
        theData.Rows.Add(aRow)


        ' load it
        Me.GridView1.DataSource = theData
        Me.GridView1.AutoGenerateColumns = False

        ' format the columns
        FormatColumns(theData)

        ' bind it
        Me.GridView1.DataBind()

    End Sub

    Private Sub FormatColumns(ByRef tblData As DataTable)
        Dim colDataColumn As DataColumn
        For Each colDataColumn In tblData.Columns()
            Me.GridView1.Columns.Add(CreateBoundColumns(colDataColumn))
        Next
    End Sub

    Private Function CreateBoundColumns(ByRef colDataColumn As DataColumn) As BoundField
        Dim bndColumn As New BoundField()
        bndColumn.DataField = colDataColumn.ColumnName
        bndColumn.HtmlEncode = False
        bndColumn.HeaderText = colDataColumn.ColumnName.Replace("_", " ")
        bndColumn.DataFormatString = SetFormatString(colDataColumn)
        Return bndColumn
    End Function
   
    Private Function SetFormatString(ByRef colDataColumn As DataColumn) As String
        Dim strDataType As String
        Select Case colDataColumn.DataType.ToString()
            Case "System.Int32"
                strDataType = "{0:#,###}"
            Case "System.Decimal"
                strDataType = "{0:C}"
            Case "System.DateTime"
                strDataType = "{0:dd-mm-yyyy}"
            Case "System.String"
                strDataType = ""
            Case Else
                strDataType = ""
        End Select
        Return strDataType
    End Function

   
Random Solutions  
 
programming4us programming4us