[英]How do I set formatting for a dynamically added column in Infragistics WebDataGrid?
I'm currently working with a utility that was written using the Infragistics WebDataGrid control.我目前正在使用使用 Infragistics WebDataGrid 控件编写的实用程序。 The tool allows a user to select a table from a drop-down list at which point it runs a stored procedure to get data from SQL Server which it then binds to the grid.
该工具允许用户从下拉列表中选择一个表,然后运行存储过程从 SQL Server 获取数据,然后将其绑定到网格。
Everything gets a default format, which for datetime columns is simply MM/DD/YYYY
in this case.一切都有一个默认格式,在这种情况下,日期时间列只是
MM/DD/YYYY
。 There is a request to include the time component for at least one of these columns.请求包含至少这些列中的一列的时间分量。
After much searching I've tried to add code to the _InitializeRow
event handler for the grid, but I can't seem to get it quite right.经过多次搜索,我尝试将代码添加到网格的
_InitializeRow
事件处理程序中,但我似乎无法完全正确。 Here's what I've come up with:这是我想出的:
if (e.Row.Index == 0)
{
foreach (GridRecordItem field in e.Row.Items)
{
if (field.Column.Key == "InsertedOnCC")
field.Column.FormatValue = "{0:g}";
}
}
This gives me an error that FormatValue
cannot have a value assigned because it is a method group.这给了我一个错误,即
FormatValue
不能分配值,因为它是一个方法组。
This was pieced together from several sources, none of which did quite what I wanted to do.这是从几个来源拼凑起来的,没有一个能完全达到我想要的效果。 For example, one piece of sample code just always assumed that the column in question was the first column, so I had to add the
foreach
loop.例如,一段示例代码总是假设有问题的列是第一列,所以我不得不添加
foreach
循环。
I've seen a lot of mention of BoundDataField
and DataFormatString
, but I can't seem to actually find how to access those.我已经看到很多人提到
BoundDataField
和DataFormatString
,但我似乎无法真正找到如何访问它们。
NOTE: I'm actually a SQL Developer, not a C# developer, so please be gentle.注意:我实际上是一名 SQL 开发人员,而不是 C# 开发人员,所以请保持温和。 :)
:)
Thanks!谢谢!
I was able to sort this out by casting the Column
as a BoundDataField
then using the DataFormatString
from there: 我能够通过将
Column
为BoundDataField
然后从那里使用DataFormatString
来解决此问题:
if (e.Row.Index == 0)
{
foreach (GridRecordItem gri in e.Row.Items)
{
BoundDataField field = gri.Column as BoundDataField;
if (field.Key == "InsertedOnCC")
field.DataFormatString = "{0:g}";
}
}
using the info above i was able to get this to work.使用上面的信息,我能够让它工作。 cleaner.
清洁工。 does not require the loop:
不需要循环:
TryCast(.Rows(0).Items.FindItemByKey("MyColumnName").Column, Infragistics.Web.UI.GridControls.BoundDataField).DataFormatString = "{0:G}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.