I have not even an idea where to look to fix this error.
Recently i get following exception after i've clicked the checkbox in a DataGridViewCheckBoxColumn
to check it and leave that cell:
System.FormatException: "" is not valid for Boolean
Here's the complete error dialog from the DataGridView
:
I even don't know which event i could handle to find the reason for this issue. The Validating
and the CellFormatting
events are triggered before the error, but both run through.
If i handle the DataError
-event i still can't figure it out. The DataGridViewDataErrorEventArgs
argument contains following informations(among others):
e.ColumnIndex = 0
e.RowIndex = 0
e.Context = Commit
The full exception(e.Exception.ToString()
) is:
System.FormatException: is not a valid value for Boolean. ---> System.FormatException: String was not recognized as a valid Boolean. at System.Boolean.Parse(String value) at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) --- End of inner exception stack trace --- at System.ComponentModel.BooleanConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at System.ComponentModel.TypeConverter.ConvertFrom(Object value) at System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)
Here's a screenshot of the relevant column properties, the column has ThreeState=false
and nothing specified for FalseValue
, TrueValue
or IndeterminateValue
:
The datasource of the BindingSource
is a List<ErpService.ArrivalChargeAssignment>
where ArrivalChargeAssignment
is a class from my WCF webservice with a bool
-field IsAssigned
, so it can never be null
(or even an empty string).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With