fun*_*err 5 c# datagridview column-types
我需要一种可以在运行时定义列类型的方法.
这是我的代码:
foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
???
//i.e. column.type = checkbox
}
Run Code Online (Sandbox Code Playgroud)
如何在此foreach循环中定义列类型?
我不是100%确定我理解你的问题,但您可以在创建列时指定列类型:
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);
}
Run Code Online (Sandbox Code Playgroud)
如果您需要在创建后更改类型 - 您不能这样做.您可以做的最好的事情是删除列并使用新类型重新创建它们.
小智 6
如果我们考虑你的榜样;
foreach (DataGridViewColumn column in this.dataGrid.Columns)
{
column.ValueType = typeof(DateTime);
}
Run Code Online (Sandbox Code Playgroud)
但是,假设您不希望datagridview中的所有列具有相同的类型;
this.datagrid.Columns[0].ValueType = typeof(Int32);
this.datagrid.Columns[1].ValueType = typeof(String);
this.datagrid.Columns[2].ValueType = typeof(DateTime);
Run Code Online (Sandbox Code Playgroud)
当您使用数据源而不是以编程方式添加自己的列时,这尤其有用.
| 归档时间: |
|
| 查看次数: |
71224 次 |
| 最近记录: |