GridView not applying DataFormatString?

C

Cesar Ronchese

Hello again! :)

I'm trying apply a money format in a column in my gridview, but it doesn't work. See the sample:

'create a datatable
Dim dtb As New Data.DataTable
Dim dtr As Data.DataRow
dtb.Columns.Add("col1", GetType(Double))
'add rows to the datatable
dtr = dtb.NewRow
dtr.Item("col1") = 100.13
dtb.Rows.Add(dtr)
dtr = dtb.NewRow
dtr.Item("col1") = 210.27
dtb.Rows.Add(dtr)
'create a column
Dim cCol As BoundField
cCol = New BoundField
'set the column
cCol.DataField = "col1"
cCol.HeaderText = "This column should be formatted"
cCol.DataFormatString = "{0:C2}"
'start the grid
GridView1.Columns.Add(cCol)
GridView1.AutoGenerateColumns = False
GridView1.DataSource = dtb
GridView1.DataBind()


What I'm missing?

PS: I'm using VB and ASP.Net 2005

Cesar
 
C

Cesar Ronchese

I found a manner to do, based on article
http://www.c-sharpcorner.com/Code/2003/June/DataFormatString.asp

Although this manner can take processing to do the job, it really works. Just needed to implement code in RowDataBound Grid's event, like the code below:


Protected Sub GridView1_RowDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
Handles GridView1.RowDataBound

Dim cCol As BoundField
For i As Integer = 0 To e.Row.Cells.Count - 1
cCol = GridView1.Columns.Item(i)
If cCol.DataFormatString <> "" Then
e.Row.Cells(i).Text = DataBinder.Eval(e.Row.DataItem, _
cCol.DataField, cCol.DataFormatString)
End If
cCol = Nothing
Next
End Sub

PS: the code above just complements the code I wrote in previous message.

[]s
Cesar

"Cesar Ronchese" <info||carsoftnet.com.br> wrote in message Hello again! :)

I'm trying apply a money format in a column in my gridview, but it doesn't work. See the sample:

'create a datatable
Dim dtb As New Data.DataTable
Dim dtr As Data.DataRow
dtb.Columns.Add("col1", GetType(Double))
'add rows to the datatable
dtr = dtb.NewRow
dtr.Item("col1") = 100.13
dtb.Rows.Add(dtr)
dtr = dtb.NewRow
dtr.Item("col1") = 210.27
dtb.Rows.Add(dtr)
'create a column
Dim cCol As BoundField
cCol = New BoundField
'set the column
cCol.DataField = "col1"
cCol.HeaderText = "This column should be formatted"
cCol.DataFormatString = "{0:C2}"
'start the grid
GridView1.Columns.Add(cCol)
GridView1.AutoGenerateColumns = False
GridView1.DataSource = dtb
GridView1.DataBind()


What I'm missing?

PS: I'm using VB and ASP.Net 2005

Cesar
 

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

Top