【问题标题】:Which control performs the type conversion:GridView or data source control?哪个控件执行类型转换:GridView还是数据源控件?
【发布时间】:2009-03-23 08:20:06
【问题描述】:
假设我们将 GridView 数据绑定到 ObjectDataSource 控件,然后执行查询。
A) 我意识到显示的 GridView 字段是字符串类型,但 GridView 是否也知道底层数据源值是什么类型(对于特定的 GridView 列)?
* 因此,如果数据源将使用一堆整数填充 GridView,则必须将这些整数转换为字符串,然后才能在 GridView 中显示。哪个控件将执行转换,GridView 还是 ObjectDataSource/SqlDataSource 控件?
B) 不管是GridView 还是ObjectDataSource 执行转换,该转换是如何执行的?只需在对象上调用 ToString() ?
谢谢
【问题讨论】:
标签:
c#
.net
asp.net
ado.net
【解决方案1】:
格式化(包括转换)由 GridView 完成。您可以通过查看每列的 DataFormatString 属性来看到这一点。这使用通用的 .NET 复合格式,例如 string.Format() 或 Console.Writeline()。它默认为 ToString()。但 DataFormatString 将接受 dd-mm-yyyy 作为 DateTime 列。
【解决方案2】:
以下是绑定列控件中的代码(通过反射器):
if (this.formatting.Length == 0)
{
return dataValue.ToString();
}
return string.Format(CultureInfo.CurrentCulture, this.formatting, new object[] { dataValue });
- this.formatting:格式字符串,
是 Henk 解释的。
- dataValue:一个对象变量,它
是绑定属性的值