Hard to post code, but easy to describe how to repro:
Create a new Winforms project (I used VS 2017 and .NET 4.7)
- Add a DataSet to the project.
- Within the DataSet, add a DataTable (no adapter).
- Within the DataTable, add a single column. It will default to varchar, but change it to NOT allow nulls.
- Add a Form to the project.
- Add a DataGridView to the form.
- Set the DataSource for the DGV to the DataTable in the DataSet.
- Run the program.
- Click in the single column of the single row.
- Type some content.
- Select all the content and hit backspace.
- Click in the empty second row that appeared.
What happens: A System.Data.NoNullAllowedException is raised indicating that the data column does not allow DBNull.Value.
What should happen: The column in the row bound to the first row of the dgv should be set to blank, not null.
What I've tried:
- Using a CellEndEdit handler to catch the bad value and convert it to string.Empty. This does actually fix SOME cases (when there's more than one column)
- Using a CellValidating handler - not helpful
- Using a CellFormatting handler - not helpful
- Using a CellParsing handler - not helpful
- Using an AddingNew handler on the BindingSource to supply new rows that have non-null values - not helpful
Any other ideas?
nonullallowedexception datagridview blank
. – mjwills