我正在尝试使用System.ComponentModel
属性属性,例如[Browsable(true)]
基于Datasource
定义为 a自动生成列,BindingList<clsTestRow>
但在弄清楚如何更改列类型而不为每个数据集手动插入它们时遇到问题。示例如下:
测试表格:
public partial class Form1 : Form
{
public BindingList<clsTestRow> ds = new BindingList<clsTestRow>();
public Form1()
{
InitializeComponent();
this.dgvTester.AutoGenerateColumns = true;
this.dgvTester.DataSource = this.ds;
this.ds.Add(new clsTestRow(1, DateTime.Now));
this.ds.Add(new clsTestRow(2, DateTime.Now));
this.ds.Add(new clsTestRow(3, DateTime.Now));
this.ds.Add(new clsTestRow(4, DateTime.Now));
this.ds.Add(new clsTestRow(5, DateTime.Now));
this.ds.Add(new clsTestRow(6, DateTime.Now));
this.ds.Add(new clsTestRow(7, DateTime.Now));
this.ds.Add(new clsTestRow(8, DateTime.Now));
this.ds.Add(new clsTestRow(9, DateTime.Now));
this.ds.Add(new clsTestRow(10, DateTime.Now));
this.ds.Add(new clsTestRow(11, DateTime.Now));
}
private void Form1_Load(object sender, EventArgs e)
{
this.dgvTester.Refresh();
}
}
数据项:
public class clsTestRow
{
private int id = 0;
private DateTime date = DateTime.Now;
[Browsable(true)]
public int Id { get { return this.id; } set { this.id = value; } }
[Browsable(true)]
public DateTime Date { get { return this.date; } set { this.date = value; } }
public clsTestRow(int _id, DateTime _date)
{
this.id = _id;
this.date = _date;
}
}
从此MSDN 链接中提取的自定义列。
输出如下。这在编辑时缺少日期列中的日历控件,这是预期的,因为没有连接它的接线。我试图确定如何连接它,而无需每次分配Datasource
值时手动应用架构,因为我期望有了这个AutoGenerateColumns
值,就会有一些方法可以为字符串以外的类型修改它。