在网格视图中显示包含列表的对象列表

Abi*_*tro 6 .net c# datagridview winforms c#-4.0

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

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

在运行时,当我设置DataSource一个GridView的属性的List<Links>对象时,NameURL出现,但该标签不.

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

编辑:刚才有个主意.如果我编写一个函数将其转换List<Links>为a DataTable,然后将其DataSource属性设置DataGridDataTable

这个问题是,每次对List<Links>I 进行更改时,我都必须DataTable再次生成,从性能的角度来看,这似乎并不理想.

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

谢谢,

Abijeet.

Mig*_*elo 2

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

DataGridView控件支持ComponentModel命名空间,以便您可以创建看似具有不存在属性的类。使用的机制相同PropertyGrid

示例代码

此示例是一个完全工作的 Windows 窗体类,其窗体包含DataGridView. 我使用一个列表添加一些对象,然后将其设置为DataSource属性。

该列表中的对象根本没有属性...但它们使用组件模型来描述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;
    }
}
Run Code Online (Sandbox Code Playgroud)