Formatting Gridview

Discussion in 'ASP .Net' started by colo, Dec 23, 2006.

  1. colo

    colo Guest

    Hi! I have a problem which I think has a simple solution, but I can't
    figure out why it's not working.

    Some background...
    I'm populating a GridView with info from an Access database like this:

    Public Sub LoadGrid()
    Dim ConnectionString As String =
    "Provider=Microsoft.Jet.OLEDB.4.0;
    DataSource=C:\DATABASE\NCP.mdb;"

    Dim NCPCon As New SDO.OleDbConnection(ConnectionString)
    Dim SQL As String = "SELECT * FROM TEST1_IN;"

    Dim cmd As New SDO.OleDbCommand(SQL, NCPCon)
    Dim adapter As New SDO.OleDbDataAdapter(cmd)
    Dim dsTEST As New DataSet()

    NCPCon.Open()
    adapter.Fill(dsTEST, "TEST1")
    NCPCon.Close()

    GridView1.DataSource = dsTEST.Tables("TEST1")
    GridView1.DataBind()
    Me.DataBind()
    End Sub

    Then I try to format the data by using this in the RowDataBound event:

    If e.Row.RowType = DataControlRowType.DataRow Then
    e.Row.Cells(1).Text = Format(e.Row.Cells(1).Text,
    "#,###.00")
    End if

    The problem is that when the table is rendered this generates a column
    full of #,###.00 instead of the actual values like this:

    Name Salary
    Lucy #,###.00
    John #,###.00
    Mark #,###.00
    etc...

    why could it be that the format function does not work?

    Thanks for your help!
     
    colo, Dec 23, 2006
    #1
    1. Advertising

  2. colo

    colo Guest

    I tried this and it worked...

    e.Row.Cells(4).Text = String.Format("{0:#,###}", e.Row.Cells(4).Text *
    1)
    e.Row.Cells(4).HorizontalAlign = HorizontalAlign.Right

    Guess it wasn't working because it was trying to format a value that
    was already text. This also works:

    e.Row.Cells(4).Text = Format(e.Row.Cells(4).Text * 1, "#,###")

    is there a more elagant way of doing this?
     
    colo, Dec 24, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sck10

    GridView Formatting

    sck10, Oct 7, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    16,418
    Steven Cheng[MSFT]
    Oct 18, 2004
  2. Not Me
    Replies:
    0
    Views:
    502
    Not Me
    Sep 23, 2005
  3. washoetech

    Gridview Item formatting

    washoetech, Sep 29, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    9,157
    Steven Cheng[MSFT]
    Sep 30, 2005
  4. Replies:
    2
    Views:
    4,644
  5. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    18,042
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    May 9, 2006
Loading...

Share This Page