I'm attempting to use the System.ComponentModel
property attributes such as [Browsable(true)]
to auto generate columns based on a Datasource
defined as a BindingList<clsTestRow>
but am having issues figuring out how to change the column types without manually inserting them for each data set. Example is below:
Test form:
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();
}
}
Data Item:
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;
}
}
Custom column pulled from this MSDN link.
The output is below. This lacks the calendar control in Date columns when editing, which is expected since there is no wiring to tie it in. I'm trying to determine how to connect it without manually applying a schema every time I assign a Datasource
value since I'd expect with the AutoGenerateColumns
value there would be some way to modify this for types other than strings.