Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridViewCheckBoxColumn: FormatException on boolean-column

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:

enter image description here

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:

enter image description here

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).

like image 216
Tim Schmelter Avatar asked Nov 05 '14 15:11

Tim Schmelter


2 Answers

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.

like image 167
Shell Avatar answered Oct 20 '22 13:10

Shell


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.

like image 44
TJC Avatar answered Oct 20 '22 11:10

TJC