Form designer: GridView with DropDownListBox field column: An ArgumentOutOfRangeException can occur at runtime when the drop-down list does not contain an empty string value item

Issue

When you launch a form with a GridView that has a DropDownListBox as one of the column fields, and the drop-down list field doesn’t contain an empty string value item, you might get the following .NET exception error at runtime:

[ArgumentOutOfRangeException: '[DROPDOWNLISTBOX_ID]' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +1379
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +408
   System.Web.UI.WebControls.ListControl.PerformSelect() +44
   System.Web.UI.Control.DataBindChildren() +248
   ...

This is a known behavior due to the data binding nature of the GridView and the DropDownListBox controls.

The System.ArgumentOutOfRangeException is raised when the GridView tries to bind the default value (which is an empty string) to the missing empty string value item in the drop-down list field in the GridView in Edit mode.

Solution

The solution is to always keep an empty string value item (with a Select an item text, for example) in the DropDownListBox field.