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

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

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

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

如何在gridview中的List对象中显示标记List

编辑:刚才有个主意。 如果我编写一个函数将List转换为DataTable ,然后将DataGridDataSource属性设置为DataTable ,该怎么办?

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

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

谢谢,

Abijeet。

也许这就是你想要的……一个似乎拥有更多属性的对象。

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 { 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(tags); } public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) { var listProps = new List(); // 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 tags = new List(); 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; } } 

如果您正在使用WPF,我认为您这样做,为什么不使用DataGridView行详细信息模板? 您可以将第二个网格作为与标记列表绑定的行的详细信息模板。