c# - 表示在tinyint中保存在sql server中的值的网格列和c#中的枚举未在conbobox中显示所选项目
问题描述
我已将我的枚举值保存在 sql server 中为 tinyint。我想显示这些值并使它们在 gridview 中可编辑,因此我使用组合框。这是我的枚举类。我在网上找到了这段代码。当我的模型中有类成员时,代码运行良好,但是对我必须删除的 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}"/>
推荐阅读
- angular - 创建具有标点符号支持的描述输入字段
- python - 如何检查列表是否包含空字典
- c++ - 为共享 ptr 向量实现复制 c'tor?
- python - Python Pandas 在 groupby 中的单元格中按给定值移动
- excel - 复制和重命名选项卡 VBA
- amazon-web-services - 跟踪 AWS Lambda 函数以检测人为干预和危害
- c# - 如何使用 C# 修复格式错误的空格分隔符
- rollupjs - 处理 continuation-local-storage/contex.js 时出现语法错误
- python-3.x - 向 Python 字典中的键添加多个值
- node.js - 从nodejs服务器自动删除的图像