views:

231

answers:

2

I have a DataGridView in a C# WinForms app that is DataBound at runtime (through Form_Load) to a custom Object.
In the design view of the DataGridView I have no columns set up.
When the Form loads the the columns are automatically created based on the data in the Custom object that it is DataBound to.
My question is how can I control the the Columns that are automatically created.
For example if I want one of the columns to be a DataGridViewLinkColumn instead of the DataGridViewTextBoxColumn that is automatically created?

+2  A: 

The default columns are based on the data-type. I haven't checked, but for a link you could try exposing the data as Uri, but that might be hopeful. Really, if you want a specific type of column - add the columns through code and set DataGridView.AutoGenerateColumns to false.

As Andrew implies; normally something like reflection is used to generate the columns, and you'll get a column for every (browsable + public + readable) property. There is a layer of abstraction on top of this if you need, but this won't help with adding a hyperlink column.

Marc Gravell
+1 You may want to mention that reflection is used to auto-generate the columns.
Andrew Hare
@Andrew - strictly speaking, it isn't ;-p
Marc Gravell
@Marc - Whoops, I definitely thought it was!
Andrew Hare
@Andrew - it is `System.ComponentModel`, which could mean `ITypedList`, could mean `ICustomTypeDescriptor` / `TypeDescriptionProvider`, or could be reflection (the default provider). But it can be customized in lots of fun ways.
Marc Gravell
Ah I see what you mean - thanks for explaining :)
Andrew Hare
A: 

You can pre-create your columns in the designer. If the name of the column matches the name of the property the column will end up bound to, the databinding will take care of the DGV population for you as before.

Anna Lear