Search code examples
c#.netwinformsdatagridviewdatagridviewcombobox

DataGridViewComboBox value is not valid?


I keep getting an error that states DataGridViewComboBox value is not valid. It seems like it is also in an endless loop: I will click ok and it will continuously keep popping up. I am running a program with a windows form application written in C# and .NET. Does anyone know how to fix this error?

Here is some portions of my code:

// authorityTypeDataGridViewTextBoxColumn
// 
this.authorityTypeDataGridViewTextBoxColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
this.authorityTypeDataGridViewTextBoxColumn.DataPropertyName = "AuthorityType";
this.authorityTypeDataGridViewTextBoxColumn.DataSource = this.AuthorityTypeBindingSource;
this.authorityTypeDataGridViewTextBoxColumn.DisplayMember = "Description";
this.authorityTypeDataGridViewTextBoxColumn.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
this.authorityTypeDataGridViewTextBoxColumn.Frozen = true;
this.authorityTypeDataGridViewTextBoxColumn.HeaderText = "AuthorityType";
this.authorityTypeDataGridViewTextBoxColumn.MaxDropDownItems = 100;
this.authorityTypeDataGridViewTextBoxColumn.Name = "authorityTypeDataGridViewTextBoxColumn";
this.authorityTypeDataGridViewTextBoxColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.authorityTypeDataGridViewTextBoxColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
this.authorityTypeDataGridViewTextBoxColumn.ValueMember = "Value";
this.authorityTypeDataGridViewTextBoxColumn.Width = 121;
// 
// AuthorityTypeBindingSource
// 
this.AuthorityTypeBindingSource.DataMember = "AuthorityType";
this.AuthorityTypeBindingSource.DataSource = this.lookUpDataSet;

Does anyone have any suggestions?

Here is the Handler:

private void TaskSummaryGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    MessageBox.Show(this, e.Exception.Message);
    e.Cancel = true;
}

Solution

  • It looks like your DataGridViewTextBoxColumn at some point was a DataGridViewComboBoxColumn, because you have ComboBox properties that do not belong to a TextBox column.

    The DataGridViewTextBoxColumn does not have:

    .DataSource = this.AuthorityTypeBindingSource;
    .DisplayMember = "Description";
    .DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
    .MaxDropDownItems = 100;
    .ValueMember = "Value";
    

    I can only guess editing the designer file by hand can cause this.