WinForms DataGridView - 数据绑定到具有列表属性(可变列数)的对象 [英] WinForms DataGridView - databind to an object with a list property (variable number of columns)

查看:21
本文介绍了WinForms DataGridView - 数据绑定到具有列表属性(可变列数)的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET 类,我想在 DataGridView 中显示,默认数据绑定 - 将 DGV 的数据源设置为对象 - 产生了我的 90% 的需求(即它正确输出公共属性,我可以添加轻松排序).

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.

我的类看起来像这样:

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

理想情况下,我可以将该 Rating 属性扩展为多个数字列,以便在运行时提供如下输出:

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

标题|总评分 |发生|R1 |R2 |R3 ... RN

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.

推荐答案

喜欢这个吗?

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

    }
}

这篇关于WinForms DataGridView - 数据绑定到具有列表属性(可变列数)的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆