Define DataGridView Column type Programmatically

后端 未结 6 1795
眼角桃花
眼角桃花 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:40

    I'm not 100% certain I understand you question, but you can specify the column type when you create the column:

    foreach (var definition in columnDefinitions)  // Some list of what the column types are
    {
        var columnSpec = new DataColumn
            {
                DataType = definition.Type, // This is of type System.Type
                ColumnName = defintion.Name // This is of type string
            };
    
        this.dataGrid.Columns.Add(columnSpec);
    }
    

    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.

提交回复
热议问题