Datagridview:如何“类型转换”列表中的属性
本文关键字:类型转换 属性 列表 如何 Datagridview | 更新日期: 2023-09-27 18:16:23
我目前正在尝试填充一个通用的数据视图,因为我正在努力进行类型转换。下面是我的场景:
我有一个实体,它有一个自定义enum:
public class HistoryItem
{
public virtual int Id { get; set; }
[TypeConverter(typeof(CommunicationTypeConverter))]
public virtual CommunicationType TypeOfCommunication { get; set; }
public virtual DateTime Date { get; set; }
public virtual string Remark { get; set; }
}
public enum CommunicationType
{
OutgoingCall,
OutgoingEmail
}
如你所见,我已经为类型转换标记了TypeOfCommunication属性:
public class CommunicationTypeConverter : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is CommunicationType)
{
switch((CommunicationType)value)
{
case CommunicationType.OutgoingEmail:
return "OUTGOING_EMAIL";
}
}
return base.ConvertFrom(context, culture, value);
}
}
目前我正在尝试将CommunicationType.ToString()字符串转换为自定义字符串。
我已经读过这个,我知道类型转换的概念基本上(从WPF),但我没有得到链接,当它涉及到绑定这个历史项目列表到我的datagridview:
IList<HistoryItem> history = _currentCustomer.HistoryItems;
dgvCustomerHistory.DataSource = history;
dgvCustomerHistory.Columns.Add("TypeOfCommunication", "Type");
dgvCustomerHistory.Refresh();
我的目标是用图像/图标"替换"CommunicationType字符串-只是为了让你知道我想要去哪里。
首先,您想要自动生成DataGridView
中的列吗?如果是的话,像这样编码:
IList<HistoryItem> history = _currentCustomer.HistoryItems;
dgvCustomerHistory.AutoGenerateColumns = true;
dgvCustomerHistory.DataSource = history;
如果没有,像这样写:
IList<HistoryItem> history = _currentCustomer.HistoryItems;
dgvCustomerHistory.AutoGenerateColumns = false;
DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.HeaderText = "Communication Type";
column.DataPropertyName = "TypeOfCommunication"; // Name of property
dgvCustomerHistory.Columns.Add(column);
dgvCustomerHistory.DataSource = history;
我认为你只需要在CommunicationTypeConverter
中添加更多的覆盖。您只处理了ConvertFrom
方法,您需要处理ConvertTo
方法和CanConvert
方法。
public class CommunicationTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
switch ((string)value)
{
case "OUTGOING_EMAIL":
return CommunicationType.OutgoingEmail;
default:
return CommunicationType.OutgoingCall;
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value is CommunicationType)
{
switch ((CommunicationType)value)
{
case CommunicationType.OutgoingEmail:
return "OUTGOING_EMAIL";
default:
return "OUTGOING_CALL";
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
你可以返回图像/图标而不是字符串。只要确保您更新了适当的Convert
和CanConvert
部分。