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

6

我目前正在开发一个书签管理应用程序(Windows Forms),具有高级搜索功能。

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

在运行时,当我将DataGridViewDataSource属性设置为List<Links>对象时,NameURL会显示出来,但是标签不会

我该如何在DataGridView中显示在List<Links>对象中的标签列表?

编辑:我想到了一个主意。如果我编写一个函数将List<Links>转换成DataTable,然后将DataGridDataSource属性设置为DataTable,怎么样?

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

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

谢谢,

阿比吉特。


你的GridView中有匹配Links类属性的<ItemTemplate>吗?如果没有,请将AutoGenerateColumns设置为true,GridView将会自动生成列。请发布您的HTML和用于绑定的代码。在这里查看:http://forums.asp.net/t/1109165.aspx/1 - tranceporter
很抱歉我忘了提到,这是一个WinForm应用程序。 - Abijeet Patro
1
在这种情况下,您需要创建一个DataGridViewComboBoxColumn,并将标签列表分配为数据源。然后第三列应该出现,其中包含一个组合框。请参阅此处:http://arsalantamiz.blogspot.co.uk/2008/09/binding-datagridview-combobox-column.html - tranceporter
@tranceporter,谢谢你的链接,这对将来很有帮助。但在这种情况下,我希望将列表中的每个项目显示为DataGridViewTextBox列。 - Abijeet Patro
如果您有关于如何创建书签的代码,请发送给我完整的编码部分,我不知道如何创建书签。提前致谢...我的电子邮件地址是j.s.banger@hotmail.com - j.s.banger
2个回答

2
也许这正是您想要的...一个看起来拥有更多属性的对象,实际上并没有。 DataGridView 控件支持 ComponentModel 命名空间,因此您可以创建类,这些类似于拥有不存在属性。这是与 PropertyGrid 使用相同的机制。 示例代码 此示例是一个完全工作的 Windows Forms 类,其中包含一个带有 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;
    }
}

0

如果你正在使用WPF,为什么不使用DataGridView行详细模板呢?你可以将第二个网格作为行的详细模板,然后将其与标签列表进行绑定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接