Define DataGridView Column type Programmatically

后端 未结 6 1794
眼角桃花
眼角桃花 2020-12-10 08:06

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)
{
???
/         


        
6条回答
  •  自闭症患者
    2020-12-10 08:50

    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:

    DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn()
    dataGridView1.Columns.Add(col);
    

    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.

提交回复
热议问题