在运行时设置 DataGridView 上的 DataFormatString?

发布于 2024-08-01 13:33:16 字数 68 浏览 2 评论 0原文

是否可以在运行时设置 ASP.NET DataGridView 中的列或单元格的 DataFormatString 属性?

Is it possible to set the DataFormatString property of a column or cell in an ASP.NET DataGridView at runtime?

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

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

发布评论

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

评论(3

未央 2024-08-08 13:33:16

这应该有效。

BoundField priceField = grid.Columns[0] as BoundField;
priceField.DataFormatString = "{0:c}";
priceField.HtmlEncode = false;
grid.DataSource = list;
grid.DataBind();

通过 找到链接

This should work.

BoundField priceField = grid.Columns[0] as BoundField;
priceField.DataFormatString = "{0:c}";
priceField.HtmlEncode = false;
grid.DataSource = list;
grid.DataBind();

Found via Link

○闲身 2024-08-08 13:33:16

据我所知,您可能想尝试在 RowDataBound 事件上执行列格式,尽管可能会降低性能。
如果有人可以提供更简单的方法,我会很高兴。

Not that I know of, you might want to try peforming the column formatting on RowDataBound event though might have some performance degrade.
Will be glad if someone can provide a simpler method.

忱杏 2024-08-08 13:33:16

似乎没有办法设置 DataFormatString 属性。 我最终将数据源绑定到表,然后遍历所有单元格并手动格式化它们:

DataGridView.AutoGenerateColumns = true;
DataGridView.DataSource = dbconnection.getDataReader();
DataGridView.DataBind();

int result;

for (int i = 0; i < DataGridView.Rows.Count; i++)
{
  foreach (TableCell c in DataGridView.Rows[i].Cells)
  {
    if (int.TryParse(c.Text, out result))
    {
      c.Text = String.Format("{0:n0}", result);
    }
  }
}

此方法非常适合我。 不确定它如何扩展大型数据集,尽管我猜测它会很好。

There doesn't seem to be a way to set the DataFormatString property. I have ended up binding the datasource to the table and then going through all the cells and formatting them manually:

DataGridView.AutoGenerateColumns = true;
DataGridView.DataSource = dbconnection.getDataReader();
DataGridView.DataBind();

int result;

for (int i = 0; i < DataGridView.Rows.Count; i++)
{
  foreach (TableCell c in DataGridView.Rows[i].Cells)
  {
    if (int.TryParse(c.Text, out result))
    {
      c.Text = String.Format("{0:n0}", result);
    }
  }
}

This method works perfectly for me. Not sure how it would scale up with a large dataset although my guess is it would be fine.

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