views:

41

answers:

3

Hello,

I have the following code in the columns section of my GridView:

<Columns>
      <asp:boundfield datafield="Nominal-Anual" HeaderText="Nominal anual"/>
      <asp:boundfield datafield="Anual" headertext="Anual" sortexpression="Anual" />
      <asp:boundfield datafield="Semestral" headertext="Semestral" DataFormatString="{0:0.00}" />
      <asp:BoundField DataField="Trimestral" HeaderText="Semestral" HtmlEncode="false" DataFormatString="{0:00.0000}" />
      <asp:BoundField DataField="Mensual" HeaderText="Mensual" />
</Columns>

This gridview is feed from a datatable wich all datacolumns are of string type.

My problem is that the DataFormatString is not being applied and i dont know why.

Im getting a lots of decimals in all columns included the ones with dataformatstrings.

What could be happening?

Thanks in advance.

Best Regards.

Jose

+1  A: 

Try

<asp:boundfield datafield="Semestral" headertext="Semestral" DataFormatString="{0:0.00}" HtmlEncode="false" />

I've seen this happening because (and correct me if I'm wrong) the field value is HtmlEncoded before applying the format making the format string have no effect.

Response to comment:

You can try using the RowDataBound event.

The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

Kamal
First of all thanks for the response, I've tried but didnt worked.The cause appears to be that the field of my datatable is of type string (the problem is that im forced to use string columns type on the columns of my table). Do you know in wich place is doing the formatting? this way i can try to override the method.Thanks a lot.Best Regards.Jose.
Josemalive
Hi there, see my updated answer. You can apply formatting here as it occurs just before binding to the gridview
Kamal
+2  A: 

Maybe the formatting does not work because it expects numeric / decimal values and you are sending in data of string type?

Dev F
First of all thanks for the response, I've tried but didnt worked.The cause appears to be that the field of my datatable is of type string (the problem is that im forced to use string columns type on the columns of my table). Do you know in wich place is doing the formatting? this way i can try to override the method.Thanks a lot.Best Regards.
Josemalive
Maybe if you could retrieve the data from a database View instead of a Table? And then you define this new view to return decimals, by converting your string columns to a decimal type. What database are you using, MS SQL Server?
Dev F
Im using Excel as datasource. The problem is that it returns me ranges of values as object array. Then i convert this into a datatable creating the datacolumns as string to be sure that it will not crash.
Josemalive
Oops, my bad, I assumed a database. If you don't want to change from having string columns in the datatable into having say doubles, then you could follow Kamal's suggestion and use the RowDataBound event.
Dev F
Thanks Dev F.Best Regards.
Josemalive
+1  A: 

I'm surprised you are forced to put strings in the database in place of date. By the way, to change the format:

  1. Convert the BoundFields you want to change in TemplateField (You can do it with the designer)
  2. Remove the <%# Bind(...) %>
  3. Handle the RowDataBound event and set text manually: ((TextBox)e.Row.FindControl("..")).Text = MyFormatter(e.Row.DataItem);
onof