开发者

Add own columns in a binded DataGridview

I have a DataGridView that gets a datasource assigned.

I would like to create my own columns if it's (for example) DateTime.

I found an example of how you can create a DateT开发者_开发技巧imePicker (here) (and hopefully also a NumericUpDown) to add to the datagrid, but I don't know how i can define this column to my datagrid. Any help would be greatly appreciated!


Check the last method in your example:

private void Form1_Load(object sender, EventArgs e)
{
    CalendarColumn col = new CalendarColumn();
    this.dataGridView1.Columns.Add(col);
    this.dataGridView1.RowCount = 5;
    foreach (DataGridViewRow row in this.dataGridView1.Rows)
    {
        row.Cells[0].Value = DateTime.Now;
    }
}

This is where the columns are added to the DataGridView. You can use the same way to add any column object derived from DataGridViewColumn to your grid.

[Edit]

Before binding, set the DataGridView.AutoGenerateColumns property to false and add your custom columns.

You will also have to set the DataPropertyName property for each column, to define which property will be bound to which column:

CalendarColumn col = new CalendarColumn();
col.DataPropertyName = "Date"; // if your class has a "Date" property
this.dataGridView1.Columns.Add(col);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜