以编程方式定义 DataGridView 列类型 [英] Define DataGridView Column type Programmatically

查看:28
本文介绍了以编程方式定义 DataGridView 列类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种可以在运行时定义列类型的方法.

I need a way in which I can define the column type at run-time.

这是我的代码:

foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
???
//i.e. column.type  = checkbox
}

如何在这个 foreach 循环中定义列类型?

How can I define the column type in this foreach loop?

推荐答案

我不是 100% 确定我理解您的问题,但是您可以在创建列时指定列类型:

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.

这篇关于以编程方式定义 DataGridView 列类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆