C# 在栅格视图中显示包含列表的对象列表
我目前正在开发一个具有高级搜索功能的书签管理器应用程序(Windows窗体) 我创建了一个C# 在栅格视图中显示包含列表的对象列表,c#,.net,winforms,c#-4.0,datagridview,C#,.net,Winforms,C# 4.0,Datagridview,我目前正在开发一个具有高级搜索功能的书签管理器应用程序(Windows窗体) 我创建了一个Links类,每次用户输入URL时,我都会创建一个Link对象并将详细信息存储在那里。它当前具有属性Name、URL和Tags,其中Tags是一个列表 在运行时,当我将gridview的数据源属性设置为列表对象时,名称和URL会显示,但标记不会显示 如何在gridview中显示list对象内的标记列表 编辑:刚有个主意。如果我编写一个函数将列表转换为数据表,然后将数据网格的数据源属性设置为数据表,会怎么样
Links
类,每次用户输入URL时,我都会创建一个Link对象并将详细信息存储在那里。它当前具有属性Name
、URL
和Tags
,其中Tags
是一个列表
在运行时,当我将gridview的数据源
属性设置为列表
对象时,名称
和URL
会显示,但标记不会显示
如何在gridview中显示list
对象内的标记列表
编辑:刚有个主意。如果我编写一个函数将列表
转换为数据表
,然后将数据网格
的数据源
属性设置为数据表
,会怎么样
问题是每次对列表进行更改时
我都必须再次生成数据表
,从性能角度来看,这似乎并不理想
编辑2:我希望将列表中的每个项目显示为DataGridViewTextBox列
谢谢
Abijeet.如果您在WPF中工作,就像我认为的那样,为什么不使用DataGridView行详细信息模板呢?您可以使用第二个网格作为行的详细信息模板,并将其与标记列表绑定。也许这就是您想要的。。。看起来比实际具有更多属性的对象
DataGridView
controll支持ComponentModel
名称空间,以便您可以创建似乎具有不存在的属性的类。这与PropertyGrid
使用的机制相同
示例代码
此示例是一个完全工作的windows窗体类,其中一个窗体包含一个DataGridView
。我使用一个列表将一些对象添加到is中,然后将该列表设置为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;
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公开课表格1:表格
{
公共表格1()
{
初始化组件();
this.dataGridView1.Dock=DockStyle.Fill;
}
受保护的覆盖无效加载(事件参数e)
{
基础荷载(e);
dataGridView1.DataSource=新列表
{
新MyClass(“值1”、“值2”、“值3”),
新MyClass(“值1”、“值2”),
};
}
类MyClass:CustomTypeDescriptor
{
公共MyClass(参数字符串[]标记)
{
this.tags=新列表(标签);
}
公共重写属性DescriptorCollection GetProperties(属性[]属性)
{
var listProps=新列表();
//动态添加属性
对于(int i=0;i=((MyClass)组件).tags.Count)
返回null;
return((MyClass)组件).tags[index];
}
公共重写bool IsReadOnly{get{return true;}}
公共重写类型PropertyType{get{return typeof(string);}
公共重写无效重置值(对象组件){}
公共重写void SetValue(对象组件,对象值){}
公共重写bool ShouldSerializeValue(对象组件){return false;}
}
}
私有void InitializeComponent()
{
this.dataGridView1=新DataGridView();
((ISupportInitialize)(this.dataGridView1)).BeginInit();
这个.SuspendLayout();
//dataGridView1
this.dataGridView1.ColumnHeadersHeightSizeMode=DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock=DockStyle.Fill;
this.dataGridView1.Location=新系统.Drawing.Point(0,0);
this.dataGridView1.Name=“dataGridView1”;
this.dataGridView1.Size=新系统.Drawing.Size(284262);
this.dataGridView1.TabIndex=1;
//表格1
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(284262);
this.Controls.Add(this.dataGridView1);
this.Name=“Form1”;
this.Text=“Form1”;
((ISupportInitialize)(this.dataGridView1)).EndInit();
此选项为.resume布局(false);
}
私有DataGridView dataGridView1;
}
}
您的GridView中是否有与链接类属性匹配的链接?如果不是,则为gridview设置AutoGenerateColumns=true,列应为gen