在网格视图中显示包含列表的对象列表 [英] Displaying a list of object containing a list in a grid view

查看:22
本文介绍了在网格视图中显示包含列表的对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发具有高级搜索功能的书签管理器应用程序(Windows 窗体).

I am currently working on a bookmark manager application (Windows Forms), with advanced search capabilities.

我创建了一个 Links 类,每次用户输入 URL 时,我都会创建一个 Link 对象并将详细信息存储在那里.它目前具有NameURLTags 属性,其中Tags 是一个列表.

I have created a Links class, and every time a user enters a URL, I'll create a Link object and store the details there. It currently has the properties Name, URL and Tags, where Tags is a list.

在运行时,当我将 gridview 的 DataSource 属性设置为 List 对象时,NameURL 显示但标签没有.

At runtime when I set the DataSource property of a gridview to the List<Links> objects, the Name and the URL show up but the tags do not.

如何在 gridview 中的 List 对象中显示标签列表?

How do I go about displaying the list of tags inside the List<Links> object in the gridview?

刚刚有了一个想法.如果我写一个函数把List转换成DataTable,然后设置DataGrid的DataSource属性会怎样DataTable?

Just had an idea. What if I write a function to convert the List<Links> into a DataTable, and then set the DataSource property of the DataGrid to the DataTable?

这样做的问题是,每次对 List 进行更改时,我都必须再次生成 DataTable,从性能观点.

The problem with this is that everytime a change is made to the List<Links> I will have to generate the DataTable again, which does not seem ideal from a performance point of view.

编辑 2: 我希望将列表中的每个项目显示为 DataGridViewTextBox 列.

EDIT 2: I wish to display each item in the list as a DataGridViewTextBox column.

谢谢,

阿比吉特.

推荐答案

也许这就是您想要的……一个看起来具有更多实际属性的对象.

Maybe this is what you want... an object that appears to have more properties that it actually has.

DataGridView 控件支持 ComponentModel 命名空间,因此您可以创建看起来具有不存在的属性的类.它与 PropertyGrid 使用的机制相同.

The DataGridView controll supports the ComponentModel namespace so that you can create classes that appear to have properties that don't exist. It is the same mechanism the PropertyGrid uses.

示例代码

这个示例是一个完全有效的 windows 窗体类,其中一个窗体包含一个 DataGridView.我将一些对象添加到 is,使用一个列表,然后将其设置为 DataSource 属性.

This sample is a fully working windows forms class, with a form containing a DataGridView. I add some objects to is, using a list that is then set to the DataSource property.

该列表中的对象根本没有任何属性……但它们使用组件模型向 DataGridView 描述虚拟"属性.

The objects inside that list, have no properties at all... but they use component model to describe 'virtual' properties to the DataGridView.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.dataGridView1.Dock = DockStyle.Fill;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            dataGridView1.DataSource = new List<MyClass>
                    {
                      new MyClass("value 1", "value 2", "value 3"),
                      new MyClass("value 1", "value 2"),
                    };
        }

        class MyClass : CustomTypeDescriptor
        {
            public MyClass(params string[] tags)
            {
                this.tags = new List<string>(tags);
            }

            public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
            {
                var listProps = new List<PropertyDescriptor>();

                // adding properties dynamically
                for (int i = 0; i < tags.Count; i++)
                    listProps.Add(new PropDesc("Tag" + i, i));

                return new PropertyDescriptorCollection(listProps.ToArray());
            }

            private List<string> tags = new List<string>();

            class PropDesc : PropertyDescriptor
            {
                private int index;

                public PropDesc(string propName, int index)
                    : base(propName, new Attribute[0])
                {
                    this.index = index;
                }

                public override bool CanResetValue(object component) { return false; }

                public override Type ComponentType { get { return typeof(MyClass); } }

                public override object GetValue(object component)
                {
                    if (index >= ((MyClass)component).tags.Count)
                        return null;

                    return ((MyClass)component).tags[index];
                }

                public override bool IsReadOnly { get { return true; } }

                public override Type PropertyType { get { return typeof(string); } }

                public override void ResetValue(object component) { }

                public override void SetValue(object component, object value) { }

                public override bool ShouldSerializeValue(object component) { return false; }
            }
        }

        private void InitializeComponent()
        {
            this.dataGridView1 = new DataGridView();
            ((ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // dataGridView1
            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(284, 262);
            this.dataGridView1.TabIndex = 1;
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private DataGridView dataGridView1;
    }
}

这篇关于在网格视图中显示包含列表的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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