关于类型转换的问题,在MS的MSDN上也有提到过,但是LLBLGen 要有一个现在的DLL才可以在设计状态下使用。我们只是自己写写一个DLL了,在安装目录下:\Program Files (x86)\Solutions Design\LLBLGen Pro v2.6 Demo\TypeConverters,我们只要把我写自己写好的DLL放在这里,就可以使用了。
比如一些字段我们为nvchar(1),我们存的是Y,N,那你当然是想把它转成bool型了。只有绑定了这个问题才可以解决checkbox的问题。
代码也不难,我就直接给出来了
namespace CRD.TypeConverters
{
[Description("Converter with as core type System.Boolean, for mapping a field with a .NET type System.Boolean onto a string database field")]
public class BooleanStringConverter : TypeConverter
{
/// <summary>
/// Initializes a new instance of the <see cref="BooleanStringConverter"/> class.
/// </summary>
public BooleanStringConverter()
{
}
/// <summary>
/// Returns whether this converter can convert an object of the given type to the type of this converter (Boolean).
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="sourceType">A <see cref="T:System.Type"/> that represents the type you want to convert from.</param>
/// <returns>
/// <see langword="true "/>if this converter can perform the conversion; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>Accepted types are: String</remarks>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
// any integer type is accepted. No fractional types like float/double.
switch (sourceType.FullName)
{
case "System.String":
return true;
default:
return false;
}
}
/// <summary>
/// Returns whether this converter can convert the object to the specified type.
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="destinationType">A <see cref="T:System.Type"/> that represents the type you want to convert to.</param>
/// <returns>
/// <see langword="true "/>if this converter can perform the conversion; otherwise, <see langword="false"/>.
/// </returns>
/// <remarks>Accepted types are: String. True will be converted to 1, false will be
/// converted to 0.</remarks>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
// any integer type is accepted. No fractional types like float/double.
switch (destinationType.FullName)
{
case "System.String":
return true;
default:
return false;
}
}
/// <summary>
/// Converts the given object to the type of this converter (Boolean).
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="culture">Ignored</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value, which is of type boolean.
/// </returns>
/// <exception cref="T:System.NotSupportedException">The conversion could not be performed.</exception>
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
bool toReturn = true;
switch (value.GetType().FullName)
{
case "System.String":
toReturn = ((string)value == "Y");
break;
case "System.DBNull":
toReturn = false;
break;
default:
throw new NotSupportedException("Conversion from a value of type '" + value.GetType().ToString() + "' to System.Boolean isn't supported");
}
return toReturn;
}
/// <summary>
/// Converts the given value object to the specified type
/// </summary>
/// <param name="context">Ignored</param>
/// <param name="culture">Ignored</param>
/// <param name="value">The <see cref="T:System.Object"/> to convert.</param>
/// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to.</param>
/// <returns>
/// An <see cref="T:System.Object"/> that represents the converted value. The value will be 1 if <paramref name="value"/> is true, otherwise 0
/// </returns>
/// <exception cref="T:System.ArgumentNullException">The <paramref name="destinationType"/> parameter is <see langword="null"/>.</exception>
/// <exception cref="T:System.NotSupportedException">The conversion could not be performed.</exception>
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (value == null)
{
throw new ArgumentNullException("value", "Value can't be null");
}
if (!(value is bool))
{
throw new ArgumentException("Value isn't of type boolean", "value");
}
if ((bool)value)
{
switch (destinationType.FullName)
{
case "System.String":
return (string)"Y";
case "System.DBNull":
return (string)"N";
default:
throw new NotSupportedException("Conversion to a value of type '" + destinationType.ToString() + "' isn't supported");
}
}
else
{
switch (destinationType.FullName)
{
case "System.String":
case "System.DBNull":
return (string)"N";
default:
throw new NotSupportedException("Conversion to a value of type '" + destinationType.ToString() + "' isn't supported");
}
}
}
/// <summary>
/// Creates an instance of the Type that this <see cref="T:System.ComponentModel.TypeConverter"/> is associated with (bool)
/// </summary>
/// <param name="context">ignored.</param>
/// <param name="propertyValues">ignored.</param>
/// <returns>
/// An <see cref="T:System.Object"/> of type bool. It always returns 'true' for this converter.
/// </returns>
public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return true;
}
}
}
我这里只举一个的类型写法,至于其它的我就上传了。下载
至于它的解释网上看看就可以了,我就不费话了。