表示保存在tinyint中的sql server和c#中的enum中的值的网格列在conbobox中未显示所选项目



我已经在sql server中将枚举的值保存为tinyint。我想在gridview中显示这些值并使其可编辑,因为我使用的是组合框。这是我的enum类。我在网上找到了这个代码。当我的模型中有类成员时,代码工作得很好,但对sql的异常,我不得不删除。

namespace WpfApp1.Enum_
{
public class EnumBindingSourceExtension : MarkupExtension
{
private Type _enumType;
public Type EnumType
{
get { return this._enumType; }
set
{
if (value != this._enumType)
{
if (null != value)
{
Type enumType = Nullable.GetUnderlyingType(value) ?? value;
if (!enumType.IsEnum)
throw new ArgumentException("Type must be for an Enum.");
}
this._enumType = value;
}
}
}
public EnumBindingSourceExtension() { }
public EnumBindingSourceExtension(Type enumType)
{
this.EnumType = enumType;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (null == this._enumType)
throw new InvalidOperationException("The EnumType must be specified.");
Type actualEnumType = Nullable.GetUnderlyingType(this._enumType) ?? this._enumType;
Array enumValues = System.Enum.GetValues(actualEnumType);
if (actualEnumType == this._enumType)
return enumValues;
Array tempArray = Array.CreateInstance(actualEnumType, enumValues.Length + 1);
enumValues.CopyTo(tempArray, 1);
return tempArray;
}
}
public class EnumDescriptionTypeConverter : EnumConverter
{
public EnumDescriptionTypeConverter(Type type)
: base(type)
{
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value != null)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
if (fi != null)
{
var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return ((attributes.Length > 0) && (!String.IsNullOrEmpty(attributes[0].Description))) ? attributes[0].Description : value.ToString();
}
}
return string.Empty;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public enum MyType : byte
{
Undeterment,
Mixed,
Non_Calcified,
Calcified,
};

}

<DataGridTemplateColumn Header="Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox  Name="hi" ItemsSource="{Binding Source={loc:EnumBindingSource {x:Type loc:MyType}}}"   SelectedItem="{Binding RType, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
[Table("ReportDetail")]
public partial class ReportDetail {
[Key]
public int RDId { get; set; }
public int? RId { get; set; }
public byte? Segment { get; set; }
public byte? RType { get; set; }
}

但代码没有显示我从数据库中读取的所选项目。

我提出的解决方案,用于更改查询结果,调用类的构造函数,并将组合框值分配给类中的枚举值

public ReportDetail(int rDId, int? rId, byte? segment, byte? rType, byte? stenosis, int? x, int? y, int? rLength = 0)
{
RDId = rDId;
RId = rId;
Segment = segment;
RType = rType;
Stenosis = stenosis;
X = x;
Y = y;
RLength = rLength;
Type =(MyType)rType;
}
<ComboBox  Name="hi" ItemsSource="{Binding Source={loc:EnumBindingSource {x:Type loc:MyType}}}"   SelectedItem="{Binding Type, UpdateSourceTrigger=PropertyChanged}"/>

最新更新