在运行时转换一个DataGridView细胞类型 [英] Convert a DataGridView Cell type at runtime

查看:245
本文介绍了在运行时转换一个DataGridView细胞类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的datagridview都有一个复选框列仅适用于在它显示数据的一些记录。有关该复选框无效我想显示什么记录 - 一个空白文本框例如:



因此,我需要动态地在运行时更改单元格类型从复选框单元格的文本框细胞。我不能简单地改变列数据类型作为列需要有文本框和复选框细胞类型的混合。



到目前为止,我有下面的代码。

  this.deviceList1.DeviceGrid [colIndex和rowIndex位置] =新KryptonDataGridViewTextBoxCell()
this.deviceList1.DeviceGrid [colIndex和rowIndex] .ReadOnly = TRUE;



Howver这会产生一个DataGridView数据错误:



< BLOCKQUOTE>

System.FormatException:单元格格式化
值都有一个错误的类型



解决方案

排序它。很简单。解决方案是保证Value属性给出一个适当值。也就是说,它需要将其设置为一个字符串值。

  this.deviceList1.DeviceGrid [colIndex和rowIndex位置] =新KryptonDataGridViewTextBoxCell() 
this.deviceList1.DeviceGrid [colIndex和rowIndex位置] .ReadOnly = TRUE;
this.deviceList1.DeviceGrid [colIndex和rowIndex位置]。价值=;


My datagridview has a checkbox column that is only valid for some records in the data it is displaying. For the records for which a checkbox is not valid I wish to display nothing - a blank textbox for example.

Thus i need to dynamically change the cell type at run time from a checkbox cell to a textbox cell. I cannot simply change the column data type as the column needs to have a mix of textbox and checkbox cell types.

So far I have the code below.

 this.deviceList1.DeviceGrid[colIndex, rowIndex] = new KryptonDataGridViewTextBoxCell()
 this.deviceList1.DeviceGrid[colIndex, rowIndex].ReadOnly = true;

Howver this generates a DataGridView data error:

System.FormatException: Formatted value of the cell has a wrong type.

解决方案

Sorted it. Simple really. Solution is to ensure that the Value property is given an appropriate value. I.e it needs to be set to a string value.

this.deviceList1.DeviceGrid[colIndex, rowIndex] =  new KryptonDataGridViewTextBoxCell()
this.deviceList1.DeviceGrid[colIndex, rowIndex].ReadOnly = true;
this.deviceList1.DeviceGrid[colIndex, rowIndex].Value = "";

这篇关于在运行时转换一个DataGridView细胞类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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