Skip to main content
 首页 » 编程设计

c#之Datagridview:如何从列表中获取 "typeconvert"属性

2024年02月06日10lhb25

我目前正在尝试填充一个通用的数据 GridView ,因为我正在努力进行类型转换。这是我的场景:

我有一个具有自定义枚举的实体:

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() 字符串转换为自定义字符串。

我已经阅读了 this我基本上知道类型转换的概念(来自 WPF),但是在将此历史项列表绑定(bind)到我的数据 GridView 时我没有得到链接:

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 部分即可。