views:

61

answers:

1

Hi all,

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 DateTimePicker (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!

A: 

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);
Groo
But as I see it, here you define your own columns. If you bind them, te columns are autogenerated, how do I interecept that and add this type of code?
Ignace
The columns don't have to be autogenerated, you can just set `AutoGenerateColumns` to false...
Thomas Levesque