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;
}
有帮助吗?

解决方案

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.

其他提示

yeah the solution is to make datagridviewcombobox cell value the same you are getting in the code behind.

if i want to show typeof(int) value , i must set property of the datagridviewcombobox cell like:

this.ComboboxCellcolumnName.ValueType = typeof(int); 

the value type that you got(e.g int) should be the same you want to show in the combobox cell (int).

If, however, you wanted to revert back to your combo box column, you would need to set some special handling to set it up.

You can refer to the MSDN article here, or this example below:

MSDN: Binding Enums to DataGridViews

        InitializeComponent();

        // special setup for enum column
        DataGridViewComboBoxColumn stateColumn = dgLedger.Columns[0] as DataGridViewComboBoxColumn;
        if (stateColumn != null)
        {
            stateColumn.DataSource = Enum.GetValues(typeof(TransactionState));
        }
        _ledger = new BindingList<LedgerItem>();
        dgLedger.DataSource = _ledger;

I just had a similar experience with one of my datagridviews: DataError was getting thrown non-stop... It eventually turned out to be because the id in the combobox DataSource was of a different type (bigint) than the column that referenced it (int)...

I used all the solution above but none of them worked, so I tried to override the DataError event and it works very well without any problem:

private void dgv_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
 //do nothing
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top