DataGridViewCheckBoxColumn: FormatException in boolean column

I don’t even have an idea where to look for a fix for this error. I recently got the following exception after I clicked the checkbox in the DataGridViewCheckBoxColumn to check it and leave this cell:

System.FormatException: "" invalid for Boolean

Here's the full error dialog from the DataGridView :

enter image description here

I don’t even know which event I can handle to find the cause of this problem. The Validating and CellFormatting are CellFormatting before the error, but both are executed. If I handle a DataError -event, I still can't figure it out. The DataGridViewDataErrorEventArgs argument contains the following data (among others):

 e.ColumnIndex = 0 e.RowIndex = 0 e.Context = Commit 

Full exception ( e.Exception.ToString() ):

System.FormatException: is not a valid value for Boolean. ---> System.FormatException: the string was not recognized as a valid boolean. in System.Boolean.Parse (String value) in System.ComponentModel.BooleanConverter.ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value) --- End of the internal exception stack trace - at System.ComponentModel.BooleanConverter.ConvertFrom (ITypeDescript CultureInfo, object value) in System.ComponentModel.TypeConverter.ConvertFrom (object value) in System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue (Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)

Here is a screenshot of the corresponding column properties, the column has ThreeState=false and nothing is specified for FalseValue , TrueValue or IndeterminateValue :

enter image description here

The BindingSource data source is List<ErpService.ArrivalChargeAssignment> , where ArrivalChargeAssignment is a class from my WCF web service with bool -field IsAssigned , so it can never be null (or even an empty string).

+6
source share
5 answers

Ok, I did some testing with the window designer and found something strange in the code generator. So what I did in my testing,

First, I added a column of type DataGridViewCheckBoxColumn and populated the datagridview table with a data table. I added some entry with null values.

Now it worked fine and the data was displayed correctly, and also did not produce any errors. Then I changed the DefaultCellStyle property of this CheckedBoxColumn and removed the False value from the Nullvalue property and ran it again. Now the application shows this error.

I returned to this DefaultCellStyle property and returned False . then I started this project again. But still it showed me the same error.

So, we downloaded the Form.designer.cs file and checked the dataGridViewCellStyle1 object. where I found that the property is set to the string type value "False" instead of the 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 updated this line as follows and started the project again. Now the error has disappeared.

 dataGridViewCellStyle1.NullValue = false; 

When I created this DataGridViewCheckBoxColumn , I found that no cell was created by default for the property of the cell property. So, by default, the Nullvalue property was Nullvalue to False . but after changing this property, the object was created and the property is set to a string type value.

UPDATED:. This problem can be solved by re-creating this column.

+7
source

use it maybe use full

 private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { DataGridView1["Your Column Index", e.RowIndex].Value = Convert.ToBoolean(DataGridView1["Your Column Index", e.RowIndex].Value); } private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (Convert.ToBoolean(DataGridView1["Your Column Index", e.RowIndex].Value)) { DataGridView1["Your Column Index", e.RowIndex].Value = false; } else { DataGridView1["Your Column Index", e.RowIndex].Value = true ; } } 
0
source

You can also do one thing. When creating your grid, you can set the boolean value to true or false (as shown below). In the above question, the column index is 0, and when calling rowindex, you can create a grid. You will not get an error. I ran into the same problem and resolved it as shown below. Also rate the other 2 answers.

 DataGridViewCell oCell; oCell = dataGridView["Your Column Index", "Your Row index"]; oCell.Value = false; 
0
source

I just solved this in an elegant way if your database column type is logical (β€œ bit ”) and your DataGridView AutoGenerateColumns is true, then DataGridViewCheckBoxColumn , so all you have to do is change the default true and false values ​​and the data type .

General code to fix the problem , insert after defining the DataSource:

 foreach ( DataGridViewColumn dc in DataGridView1.Columns) { if (dc.ValueType == typeof(Boolean)) { dc.ValueType = typeof(Int32); ((DataGridViewCheckBoxColumn)dc).DefaultCellStyle.NullValue = 0; ((DataGridViewCheckBoxColumn)dc).TrueValue = 1; ((DataGridViewCheckBoxColumn)dc).FalseValue = 0; } } 
0
source

I 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 do not need to catch an error handler. The problem in a nutshell is that the DataGridView object adds a new row (therefore, the error is triggered during the initial paint), but the root of the problem is that the data in the new row has not yet been initialized, therefore, it is "undefined". ... so just set the default value for the Defined Value option instead of leaving it blank.

0
source

Source: https://habr.com/ru/post/977770/


All Articles