Formatting applied to boundfields in gridview is not working

半腔热情 提交于 2019-12-11 02:27:36

问题


I have the following columns in a gridview, one is a date and the other one is a dollar amount. I applied the formatting and set the HtmlEncode property to false, however the values still come up unformatted:

<asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="true" HtmlEncode="False" DataFormatString="{0:C}" />
<asp:BoundField DataField="Sale_Date" HeaderText="Sale Date" ReadOnly="true" HtmlEncode="False" DataFormatString = "{0:d}" />

This is how these values appear in the gridview:

The "Total" value comes up as: 190.0000 The Sale Date value comes up as: 9/2/2010 8:59:00 AM

Any suggestions?


回答1:


I found what the problem was. I was not able to format the boundfields from the gridview because its data type was set to a string. The data set that I am using to populate the gridview is coming from a web service, all fields in the data set are coming back as strings so that is why the DataFormatString property in the gridview was not working.

The fix: I ended up changing the data type of these fields after I got the data set from the web service and before using it as the datasource of the grid view, once I did this, the DataFormatString property in the gridview worked as expected:

ds = _ws.GetOrderList(brokerId, type, pageSize, pageNum, sort, searchBy, searchFor);
ds2 = ds.Clone();

ds2.Tables[0].Columns["Price"].DataType = System.Type.GetType("System.Double");
ds2.Tables[0].Columns["Total"].DataType = System.Type.GetType("System.Double");
ds2.Tables[0].Columns["Sale_Date"].DataType = System.Type.GetType("System.DateTime");

foreach (DataRow row in ds.Tables[0].Rows)
  {
     ds2.Tables[0].ImportRow(row);
  }
return ds2


来源:https://stackoverflow.com/questions/3629837/formatting-applied-to-boundfields-in-gridview-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!