DataGridViewCheckBoxColumn: FormatException on boolean-column
Ok, I have done some testing with windows form designer and I found something strange in code generator. So, What i have done in my testing is
First I have added a column with DataGridViewCheckBoxColumn
type and populated the datagridview
with a data table. I have add some record with null values.
Now, it was working fine and data showing correctly and also it was not giving any error.
Then I have changed the DefaultCellStyle
property of that CheckedBoxColumn
and removed False
value from Nullvalue
property and start it again.
Now, application is showing that error.
I came back to that DefaultCellStyle
property and set the False
value back. then I have run that project again. But, still it was showing me the same error.
So, loaded the Form.designer.cs
file and checked the dataGridViewCellStyle1
object. where I have found that the property is set with the string type value "False"
instead of boolean type false
.
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle1.NullValue = "False";
this.Column1.DefaultCellStyle = dataGridViewCellStyle1;
this.Column1.HeaderText = "Check Box";
this.Column1.Name = "chkCol";
So, I have updated that line as follows and started the project again. Now, the error is gone.
dataGridViewCellStyle1.NullValue = false;
When I have created that DataGridViewCheckBoxColumn
I found that there is no object is created for default cell style property. So, by default NullValue
property was taking false
value. but, after modifying that property the object has been created and the property is assigned with string type value.
UPDATED: This issue can be resolved by simply re-creating that column.
I've struggled with the same problem and have a simple fix without having to write any code.
The problem is not with the "TrueValue" or "FalseValue" parameters, and you don't need to trap the error handler. The problem in a nutshell is when the DataGridView object adds a new row (hence the error fires during the initial paint), but the root of the problem is that the data in the new row has not yet been initialized, hence it is "indeterminate"....so just set a default value for the "IndeterminateValue" instead of leaving it empty.