定义的DataGridView列类型编程 [英] Define DataGridView Column type Programmatically

查看:177
本文介绍了定义的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天全站免登陆