2011-01-17 11 views
9

मेरे पास एक .NET क्लास है जो मैं डेटाग्रिड व्यू में दिखाना चाहता हूं, और डिफ़ॉल्ट डेटाबेस - डीजीवी के डेटासोर्स को ऑब्जेक्ट में सेट करना - उत्पादन करता है मेरी आवश्यकताओं का 9 0% (यानी यह सार्वजनिक गुणों को सही तरीके से आउटपुट कर रहा है और मैं आसानी से सॉर्टिंग जोड़ सकता हूं)।WinForms DataGridView - एक सूची संपत्ति (कॉलम की चर संख्या) के साथ किसी ऑब्जेक्ट पर डेटाबेस

हालांकि, मुझे जिन गुणों को बांधने की आवश्यकता है उनमें से एक सूची है जिसमें डेटा शामिल है जो अन्य डेटाबेस आइटमों के बाद अलग-अलग कॉलम में होना चाहिए। मैं इस पर लागू हूं कि इसे कैसे कार्यान्वित किया जाए।

शीर्षक:

public class BookDetails 
{ 
    public string Title { get; set; } 
    public int TotalRating { get; set; } 
    public int Occurrence { get; set; } 
    public List<int> Rating { get; set; } 
} 

आदर्श रूप में, मैं विस्तार करने के लिए सांख्यिक स्तंभ की एक संख्या में है कि रेटिंग संपत्ति कार्यावधि में इस तरह एक आउटपुट देने के लिए सक्षम होगा:

मेरी कक्षा कुछ इस तरह दिखता | कुल रेटिंग | घटना | आर 1 | आर 2 | R3 ... आर.एन.

यह भी उपयोगी कुल रेटिंग सभी व्यक्तिगत रेटिंग की राशि के रूप में गणना की जा करने के लिए होगा, लेकिन मैं इस मुद्दे के बिना मैन्युअल को अपडेट करते हुए कर रहा हूँ इस समय।

+0

आपको इस प्रकार के लिए टाइप डिस्क्रिप्टर (या शायद टाइप कनवर्टर) को लागू करने होंगे। यदि आप जानते हैं कि क्या करना है तो काफी छोटा। दुर्भाग्यवश मैंने लिखा एक अच्छा उदाहरण अब काम पर है। – leppie

+0

@leppie - TypeConverter यहां लागू नहीं होता है; वास्तव में, ITypedList शायद सबसे आसान है; उसके बाद - TypeDescriptionProvider (चूंकि यह टाइप की गई सूची के लिए ICustomTypeDescriptor का उपयोग नहीं करेगा) –

+1

@leppie - हमें केवल दो मूर्ख होना चाहिए जो मुझे ढांचे के इस अंधेरे कोने के साथ गड़बड़ करने के लिए पर्याप्त पागल होना चाहिए; पी –

उत्तर

18

इस तरह?

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Windows.Forms; 
public class BookDetails 
{ 
    public string Title { get; set; } 
    public int TotalRating { get; set; } 
    public int Occurrence { get; set; } 
    public List<int> Rating { get; set; } 
} 
class BookList : List<BookDetails>, ITypedList 
{ 

    public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors) 
    { 
     var origProps = TypeDescriptor.GetProperties(typeof(BookDetails)); 
     List<PropertyDescriptor> newProps = new List<PropertyDescriptor>(origProps.Count); 
     PropertyDescriptor doThisLast = null; 
     foreach (PropertyDescriptor prop in origProps) 
     { 

      if (prop.Name == "Rating") doThisLast = prop; 
      else newProps.Add(prop); 
     } 
     if (doThisLast != null) 
     { 
      var max = (from book in this 
         let rating = book.Rating 
         where rating != null 
         select (int?)rating.Count).Max() ?? 0; 
      if (max > 0) 
      { 
       // want it nullable to account for jagged arrays 
       Type propType = typeof(int?); // could also figure this out from List<T> in 
               // the general case, but make it nullable 
       for (int i = 0; i < max; i++) 
       { 
        newProps.Add(new ListItemDescriptor(doThisLast, i, propType)); 
       } 
      } 
     } 
     return new PropertyDescriptorCollection(newProps.ToArray()); 
    } 

    public string GetListName(PropertyDescriptor[] listAccessors) 
    { 
     return ""; 
    } 
} 
class ListItemDescriptor : PropertyDescriptor 
{ 
    private static readonly Attribute[] nix = new Attribute[0]; 
    private readonly PropertyDescriptor tail; 
    private readonly Type type; 
    private readonly int index; 
    public ListItemDescriptor(PropertyDescriptor tail, int index, Type type) : base(tail.Name + "[" + index + "]", nix) 
    { 
     this.tail = tail; 
     this.type = type; 
     this.index = index; 
    } 
    public override object GetValue(object component) 
    { 
     IList list = tail.GetValue(component) as IList; 
     return (list == null || list.Count <= index) ? null : list[index]; 
    } 
    public override Type PropertyType 
    { 
     get { return type; } 
    } 
    public override bool IsReadOnly 
    { 
     get { return true; } 
    } 
    public override void SetValue(object component, object value) 
    { 
     throw new NotSupportedException(); 
    } 
    public override void ResetValue(object component) 
    { 
     throw new NotSupportedException(); 
    } 
    public override bool CanResetValue(object component) 
    { 
     return false; 
    } 
    public override Type ComponentType 
    { 
     get { return tail.ComponentType; } 
    } 
    public override bool ShouldSerializeValue(object component) 
    { 
     return false; 
    } 
} 
static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     var data = new BookList { 
      new BookDetails { Title = "abc", TotalRating = 3, Occurrence = 2, Rating = new List<int> {1,2,1}}, 
      new BookDetails { Title = "def", TotalRating = 3, Occurrence = 2, Rating = null }, 
      new BookDetails { Title = "ghi", TotalRating = 3, Occurrence = 2, Rating = new List<int> {3, 2}}, 
      new BookDetails { Title = "jkl", TotalRating = 3, Occurrence = 2, Rating = new List<int>()}, 
     }; 
     Application.Run(new Form 
     { 
      Controls = { 
       new DataGridView { 
        Dock = DockStyle.Fill, 
        DataSource = data 
       } 
      } 
     }); 

    } 
} 
+0

(मैंने पूरी "सूची एक्सेसर" चीज़ पर भी चमक ली है - यदि आपको * की आवश्यकता है, तो यह श्रृंखला के माध्यम से प्रत्येक कॉल का पालन करने का एक मामला है) –

+0

+1 कूल, मैंने सोचा थोड़ा सा जटिल, सूची में वस्तुओं की एक परिवर्तनीय मात्रा होने के कारण :) – leppie

+0

बिल्कुल सही! मेरे लिए एक आकर्षण की तरह काम करता है। बहुत बहुत धन्यवाद :) – vitorbal

संबंधित मुद्दे