I need a way in which I can define the column type at run-time.
Here is my code:
foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
???
//i.e. column.type = checkbox
}
How can I define the column type in this foreach loop?
I need a way in which I can define the column type at run-time.
Here is my code:
foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
???
//i.e. column.type = checkbox
}
How can I define the column type in this foreach loop?
I'm not 100% certain I understand you question, but you can specify the column type when you create the column:
If you need to change the type once it's been created - you can't do that. The best you can do is delete the columns and recreate them with the new types.
Assuming your'e using BindingSource
you can assign button column also and you can assign properties also like this
I assume you mean when you create the DataGridView. In that case you can define it in a DataTable and hook it up to the dgv's Datasource:
For example:
If we consider your example;
But, assuming you do not want all the columns in your datagridview to be of the same type;
This is especially useful when you are using a datasource and not adding your own columns programatically.
You can't change the type of a DataGridView column after it is created but there is nothing to stop you creating columns as needed at run-time.
So, depending on the logic the determines the type of each column, you create columns as needed and add them to the DataGridView.
An example of creating a checkbox column is below:
Without any more information on what determines your column types it is hard to give more advice, but you could easily use this technique with a DataTable, inspecting the type of each of its columns, or even using reflection over an object you are binding the datagridview to.