类型转换器在绑定到ASP.NET的GridView [英] TypeConverter when binding to ASP.NET GridView
问题描述
我试图将对象绑定到一个GridView的DataSource的一个IList和对象的属性之一是枚举。我试图用的TypeConverter在枚举使用说明当对象绑定到GridView行。它看起来并不像我 EnumConverter.ConvertTo
方法被调用。将一个类型转换器被自动调用当对象被绑定到一个ASP.NET的GridView?
ENUM:
[类型转换器(typeof运算(AuditReasonConverter))]
公共枚举AuditReason
{
[System.ComponentModel.Description(成功登录)
SuccessfulLogin,
[System.ComponentModel.Description(登录失败)
FailedLogin,
[System.ComponentModel.Description(新用户)]
新用户,
[System.ComponentModel.Description(编辑用户)]
EditedUser
}
类型转换器类:
公共类AuditReasonConverter:EnumConverter
{
公共AuditReasonConverter()
: 基础(
typeof运算(Blah.Core.AuditItem.AuditReason))
{}
公众覆盖对象的ConvertTo(ITypeDescriptorContext的背景下,
System.Globalization.CultureInfo文化,对象的值,
System.Type的destinationType)
{
如果(destinationType == typeof运算(字符串))
{
返回Utilities.GetEnumerationDescription(typeof运算(Blah.Core.AuditItem.AuditReason),价值); //你的code在这里
}
返回base.ConvertTo(上下文,文化,价值,destinationType);
}
}
没有,GridView控件似乎只是去的ToString。
我所做的不过是子类绑定列(或的DataControlField =更多的工作),并使用在FormatDataValue你的转换器 -
公共类ConverterBoundField:绑定列
{
保护覆盖字符串FormatDataValue(对象dataValue,布尔EN code)
{
的TypeConverter变换器= TypeDescriptor.GetConverter(dataValue.GetType());
如果(converter.CanConvertTo(typeof运算(字符串)))
{
返回converter.ConvertToString(dataValue);
}
返回base.FormatDataValue(dataValue,EN code);
}
}
您也许应该尊重EN code参数,并做所指定的任何格式...这可能是最好的实现CanConvertTo你的转换器还。
I am trying to bind an IList of objects to a GridView's DataSource and one of the properties of the object is an enum. I was trying to use a TypeConverter on the enum to use a Description when the object is bound to the GridView Row. It does not look like my EnumConverter.ConvertTo
method is being called. Will a TypeConverter be called automatically when the object is being bound to an ASP.NET GridView?
ENUM:
[TypeConverter(typeof(AuditReasonConverter))]
public enum AuditReason
{
[System.ComponentModel.Description("Successful Login")]
SuccessfulLogin,
[System.ComponentModel.Description("Failed Login")]
FailedLogin,
[System.ComponentModel.Description("New User")]
NewUser,
[System.ComponentModel.Description("Edited User")]
EditedUser
}
TypeConverter Class:
public class AuditReasonConverter : EnumConverter
{
public AuditReasonConverter()
: base(
typeof(Blah.Core.AuditItem.AuditReason))
{ }
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value,
System.Type destinationType)
{
if (destinationType == typeof(string))
{
return Utilities.GetEnumerationDescription(typeof(Blah.Core.AuditItem.AuditReason), value); // your code here
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
No, GridView seems to just go for ToString.
What I have done though is subclass BoundField (or DataControlField = more work) and use your converter in FormatDataValue -
public class ConverterBoundField : BoundField
{
protected override string FormatDataValue(object dataValue, bool encode)
{
TypeConverter converter = TypeDescriptor.GetConverter(dataValue.GetType());
if (converter.CanConvertTo(typeof(string)))
{
return converter.ConvertToString(dataValue);
}
return base.FormatDataValue(dataValue, encode);
}
}
You should probably respect the encode parameter, and do any formatting that was specified... and it is probably best to implement CanConvertTo for your converter also.
这篇关于类型转换器在绑定到ASP.NET的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!