应用于 gridview 中的边界字段的格式不起作用

发布于 2024-09-17 11:21:59 字数 500 浏览 5 评论 0原文

我在网格视图中有以下列,一列是日期,另一列是美元金额。我应用了格式并将 HtmlEncode 属性设置为 false,但是这些值仍然未格式化:

<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}" />

这就是这些值在 gridview 中的显示方式:

“Total”值显示为:190.0000 销售日期值显示为:9/2/2010 8:59:00 AM

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以为你会在 2024-09-24 11:21:59

我发现问题是什么了。我无法格式化 gridview 中的边界字段,因为它的数据类型设置为字符串。我用来填充 gridview 的数据集来自 Web 服务,数据集中的所有字段都以字符串形式返回,这就是 gridview 中的 DataFormatString 属性不起作用的原因。

修复:在从 Web 服务获取数据集之后,在将其用作网格视图的数据源之前,我最终更改了这些字段的数据类型,一旦我这样做了,DataFormatString 属性在 gridview 中按预期工作:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文