Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a DataGridView Cell type at runtime

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.

like image 727
Kildareflare Avatar asked May 17 '11 16:05

Kildareflare


1 Answers

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 = "";
like image 127
Kildareflare Avatar answered Sep 28 '22 03:09

Kildareflare