12

I have a .NET class I'd like to show in a DataGridView, and the default databinding - setting the DGV's DataSource to the object - produces 90% of my requirements (i.e. it's outputting the public properties correctly and I can add sorting easily).

However, one of the properties I need to bind is a List which contains data which needs to be in separate columns after the other databound items. I'm stuck on how best to implement this.

My class looks something like this:

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

Ideally, I'd be able to expand that Rating property into a number of numeric columns to give an output like this at runtime:

Title | Total Rating | Occurrence | R1 | R2 | R3 ... RN

It would also be useful to have Total Rating be calculated as the sum of all the individual ratings, but I'm updating that manually at the moment without issue.

7
  • You are going to have to implement a TypeDescriptor (or maybe TypeConverter) for the type. Quite trivial if you know what to do. Unfortunately a nice example I have written is at work now. Commented Jan 17, 2011 at 18:08
  • @leppie - TypeConverter doesn't apply here; actually, ITypedList is probably the easiest; after that - TypeDescriptionProvider (since it won't use ICustomTypeDescriptor for a typed list) Commented Jan 17, 2011 at 19:37
  • 1
    @leppie - we must be the only two fools I know mad enough to mess with this dark corner of the framework ;p Commented Jan 17, 2011 at 19:59
  • (you can also make it read-write, but that gets a little messy because you need to know per-row the length of the list; a bit of a pain...) Commented Jan 17, 2011 at 20:02
  • @ Marc Gravell: Heh :) IIRC, my solution simply inherits from BindingSource. Seems the same as your answer. Commented Jan 17, 2011 at 20:06

1 Answer 1

23

Like this?

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
                }
            }
        });

    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

(I've also glossed over the whole "list accessor" thing - if you need that, it it just a case of following each call through the chain)
+1 Cool, slightly more complicated than I thought, due to having a variable amount of items in the List :)
this solution is very interesting. I'm going to have to investigate ITypedList and the whole PropertyDescriptor/TypeDescriptor field. I'm running through the code in the debugger and learning a lot, so many thanks :)
Excellent solution and it provides a great example to build on. Thanks.
After all this years... THANK YOU!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.