2009-10-08 24 views
11

यह How to bind a custom Enum description to a DataGrid पर एक समान प्रश्न है, लेकिन मेरे मामले में मेरे पास कई गुण हैं।डेटा ग्रिड एनम गुणों को ग्रिड और डिस्प्ले विवरण

public enum ExpectationResult 
{ 
    [Description("-")] 
    NoExpectation, 

    [Description("Passed")] 
    Pass, 

    [Description("FAILED")] 
    Fail 
} 

public class TestResult 
{ 
    public string TestDescription { get; set; } 
    public ExpectationResult RequiredExpectationResult { get; set; } 
    public ExpectationResult NonRequiredExpectationResult { get; set; } 
} 

मैं एक WinForms DataGridView करने के लिए एक BindingList <TestResult> बाध्यकारी कर रहा हूँ (वास्तव में एक DevExpress.XtraGrid.GridControl, लेकिन एक सामान्य समाधान अधिक व्यापक रूप से लागू किया जाएगा)। मैं एनम नामों के बजाय विवरण दिखाना चाहता हूं। मैं यह कैसे हासिल कर सकता हूं? (कक्षा/enum/विशेषताओं पर कोई बाधा नहीं है; मैं उन्हें इच्छानुसार बदल सकता हूं।)

उत्तर

10

TypeConverter आमतौर पर नौकरी करेगा; यहां कुछ कोड है जो DataGridView के लिए काम करता है - विवरण पढ़ने के लिए बस अपने कोड में जोड़ें (प्रतिबिंब आदि के माध्यम से - मैंने अभी कस्टम कोड को दिखाने के लिए स्ट्रिंग उपसर्ग का उपयोग किया है)।

नोट करें कि आप शायद ConvertFrom को ओवरराइड करना चाहते हैं। कनवर्टर या पर संपत्ति स्तर (यदि आप केवल कुछ गुणों के लिए आवेदन करना चाहते हैं) पर निर्दिष्ट किया जा सकता है, और अगर आपके नियंत्रण में नहीं है तो रनटाइम पर भी लागू किया जा सकता है।

using System.ComponentModel; 
using System.Windows.Forms; 
[TypeConverter(typeof(ExpectationResultConverter))] 
public enum ExpectationResult 
{ 
    [Description("-")] 
    NoExpectation, 

    [Description("Passed")] 
    Pass, 

    [Description("FAILED")] 
    Fail 
} 

class ExpectationResultConverter : EnumConverter 
{ 
    public ExpectationResultConverter() 
     : base(
      typeof(ExpectationResult)) 
    { } 

    public override object ConvertTo(ITypeDescriptorContext context, 
     System.Globalization.CultureInfo culture, object value, 
     System.Type destinationType) 
    { 
     if (destinationType == typeof(string)) 
     { 
      return "abc " + value.ToString(); // your code here 
     } 
     return base.ConvertTo(context, culture, value, destinationType); 
    } 
} 

public class TestResult 
{ 
    public string TestDescription { get; set; } 
    public ExpectationResult RequiredExpectationResult { get; set; } 
    public ExpectationResult NonRequiredExpectationResult { get; set; } 

    static void Main() 
    { 
     BindingList<TestResult> list = new BindingList<TestResult>(); 
     DataGridView grid = new DataGridView(); 
     grid.DataSource = list; 
     Form form = new Form(); 
     grid.Dock = DockStyle.Fill; 
     form.Controls.Add(grid); 
     Application.Run(form); 
    } 
} 
+0

धन्यवाद मार्क! हमारे EnumHelper (रैली25rs के उत्तर के पहले भाग के समान) के साथ संयुक्त, यह सुरुचिपूर्ण समाधान खूबसूरती से काम करता है - डेटाग्रिड व्यू में। दुर्भाग्य से मैंने पाया कि DevExpress.XtraGrid.GridControl ** ** ** टाइपकॉन्टर विशेषता का पता नहीं लगाता है। आह। लेकिन आपका जवाब स्पष्ट रूप से सही है। – TrueWill

+1

... और आपने मुझे सही दिशा में इंगित किया। मैंने पाया कि डेवलपर एक्सप्रेस इस का समर्थन करने की योजना नहीं बना रहा है, और यह कामकाज प्रदान करता है: http://www.devexpress.com/Support/Center/p/CS2436.aspx – TrueWill

5

मुझे यकीन है कि यह कितना मदद करता है नहीं कर रहा हूँ, लेकिन मुझे लगता है कि इस तरह दिखता है Enum पर एक विस्तार विधि का उपयोग करें:

/// <summary> 
    /// Returns the value of the description attribute attached to an enum value. 
    /// </summary> 
    /// <param name="en"></param> 
    /// <returns>The text from the System.ComponentModel.DescriptionAttribute associated with the enumeration value.</returns> 
    /// <remarks> 
    /// To use this, create an enum and mark its members with a [Description("My Descr")] attribute. 
    /// Then when you call this extension method, you will receive "My Descr". 
    /// </remarks> 
    /// <example><code> 
    /// enum MyEnum { 
    ///  [Description("Some Descriptive Text")] 
    ///  EnumVal1, 
    /// 
    ///  [Description("Some More Descriptive Text")] 
    ///  EnumVal2 
    /// } 
    /// 
    /// static void Main(string[] args) { 
    ///  Console.PrintLine(MyEnum.EnumVal1.GetDescription()); 
    /// } 
    /// </code> 
    /// 
    /// This will result in the output "Some Descriptive Text". 
    /// </example> 
    public static string GetDescription(this Enum en) 
    { 
     var type = en.GetType(); 
     var memInfo = type.GetMember(en.ToString()); 

     if (memInfo != null && memInfo.Length > 0) 
     { 
      var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); 
      if (attrs != null && attrs.Length > 0) 
       return ((DescriptionAttribute)attrs[0]).Description; 
     } 
     return en.ToString(); 
    } 

आप अपने वस्तु पर कोई कस्टम गुण गेटर इस्तेमाल कर सकते हैं वापस जाने के लिए नाम:

public class TestResult 
{ 
    public string TestDescription { get; set; } 
    public ExpectationResult RequiredExpectationResult { get; set; } 
    public ExpectationResult NonRequiredExpectationResult { get; set; } 

    /* *** added these new property getters *** */ 
    public string RequiredExpectationResultDescr { get { return this.RequiredExpectationResult.GetDescription(); } } 
    public string NonRequiredExpectationResultDescr { get { return this.NonRequiredExpectationResult.GetDescription(); } } 
} 

फिर "RequiredExpectationResultDescr" और "NonRequiredExpectationResultDescr" गुण के लिए अपने ग्रिड बाँध।

कि एक छोटे से अधिक जटिल हो सकता है, लेकिन इसकी 1 बात मैं :)

+0

+1 एक अच्छे सुझाव के लिए - धन्यवाद; हमारे पास पहले से ही आपके उदाहरण की तरह एनमहेल्पर क्लास है, और एक अन्य डेवलपर ने स्ट्रिंग गुणों का सुझाव दिया है, लेकिन मैं आलसी हूं। ;) – TrueWill

2

दो अन्य उत्तर के आधार पर के साथ आया था, मैं एक साथ एक वर्ग है कि सामान्य रूप से एक मनमाना enum और बीच में बदल सकते हैं डाल दिया है प्रत्येक enum मान पर एक वर्णन विशेषता का उपयोग कर एक स्ट्रिंग।

यह विवरण एट्रिब्यूट की परिभाषा के लिए System.ComponentModel का उपयोग करता है, और केवल टी और स्ट्रिंग के बीच रूपांतरण का समर्थन करता है।

public class EnumDescriptionConverter<T> : TypeConverter 
{ 
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
    { 
     return (sourceType == typeof(T) || sourceType == typeof(string)); 
    } 

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
    { 
     return (destinationType == typeof(T) || destinationType == typeof(string)); 
    } 

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) 
    { 
     Type typeFrom = context.Instance.GetType(); 

     if (typeFrom == typeof(string)) 
     { 
      return (object)GetValue((string)context.Instance); 
     } 
     else if (typeFrom is T) 
     { 
      return (object)GetDescription((T)context.Instance); 
     } 
     else 
     { 
      throw new ArgumentException("Type converting from not supported: " + typeFrom.FullName); 
     } 
    } 

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) 
    { 
     Type typeFrom = value.GetType(); 

     if (typeFrom == typeof(string) && destinationType == typeof(T)) 
     { 
      return (object)GetValue((string)value); 
     } 
     else if (typeFrom == typeof(T) && destinationType == typeof(string)) 
     { 
      return (object)GetDescription((T)value); 
     } 
     else 
     { 
      throw new ArgumentException("Type converting from not supported: " + typeFrom.FullName); 
     } 
    } 

    public string GetDescription(T en) 
    { 
     var type = en.GetType(); 
     var memInfo = type.GetMember(en.ToString()); 

     if (memInfo != null && memInfo.Length > 0) 
     { 
      var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); 
      if (attrs != null && attrs.Length > 0) 
       return ((DescriptionAttribute)attrs[0]).Description; 
     } 
     return en.ToString(); 
    } 

    public T GetValue(string description) 
    { 
     foreach (T val in Enum.GetValues(typeof(T))) 
     { 
      string currDescription = GetDescription(val); 
      if (currDescription == description) 
      { 
       return val; 
      } 
     } 

     throw new ArgumentOutOfRangeException("description", "Argument description must match a Description attribute on an enum value of " + typeof(T).FullName); 
    } 
} 
संबंधित मुद्दे