Formatting Gridview

C

colo

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!
 
C

colo

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top