Combox in datagridview causes exception
-
Run code, press down arrow, up arrow, down arrow. Invalid operatoin exception occurs. How to fix ? Andrus.
using System; using System.Windows.Forms; using System.ComponentModel; class Supplier { public string Id { get; set; } } class Form1 : Form { [STAThread] static void Main() { Application.Run(new Form1()); } public Form1() { DataGridView grid = new DataGridView(); // if this line is commented out, all is OK: grid.EditMode = DataGridViewEditMode.EditOnEnter; ComboBoxColumn comboBoxColumn = new ComboBoxColumn(); ComboBoxCell ComboBoxCell = new ComboBoxCell(); comboBoxColumn.CellTemplate = ComboBoxCell; grid.Columns.Add(comboBoxColumn); BindingList<Supplier> l = new BindingList<Supplier>(); l.Add(new Supplier()); grid.DataSource = l; Controls.Add(grid); } class ComboBoxColumn : DataGridViewComboBoxColumn { } class ComboBoxCell : DataGridViewComboBoxCell { public override Type EditType { get { return typeof(ComboBoxEditingControl); } } } class ComboBoxEditingControl : ComboBox, IDataGridViewEditingControl { protected int rowIndex; protected DataGridView dataGridView; protected bool valueChanged = false; protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); NotifyDataGridViewOfValueChange(); } protected virtual void NotifyDataGridViewOfValueChange() { valueChanged = true; if (dataGridView != null) { dataGridView.NotifyCurrentCellDirty(true); } } public Cursor EditingPanelCursor { get { return Cursors.IBeam; } } public DataGridView EditingControlDataGridView { get { return dataGridView; } set { dataGridView = value; } } public object EditingControlFormattedValue { set { if (value.ToString() != Text) { Text = value.ToString(); NotifyDataGridViewOfValueChange(); } } get { return Text; } } public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) { return Text; } public void PrepareEditingControlForEdit(bool selectAll) { } public bool RepositionEditingControlOnValueChange { get { return false; } } public int EditingControlRowIndex { get { return rowInd
-
Run code, press down arrow, up arrow, down arrow. Invalid operatoin exception occurs. How to fix ? Andrus.
using System; using System.Windows.Forms; using System.ComponentModel; class Supplier { public string Id { get; set; } } class Form1 : Form { [STAThread] static void Main() { Application.Run(new Form1()); } public Form1() { DataGridView grid = new DataGridView(); // if this line is commented out, all is OK: grid.EditMode = DataGridViewEditMode.EditOnEnter; ComboBoxColumn comboBoxColumn = new ComboBoxColumn(); ComboBoxCell ComboBoxCell = new ComboBoxCell(); comboBoxColumn.CellTemplate = ComboBoxCell; grid.Columns.Add(comboBoxColumn); BindingList<Supplier> l = new BindingList<Supplier>(); l.Add(new Supplier()); grid.DataSource = l; Controls.Add(grid); } class ComboBoxColumn : DataGridViewComboBoxColumn { } class ComboBoxCell : DataGridViewComboBoxCell { public override Type EditType { get { return typeof(ComboBoxEditingControl); } } } class ComboBoxEditingControl : ComboBox, IDataGridViewEditingControl { protected int rowIndex; protected DataGridView dataGridView; protected bool valueChanged = false; protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); NotifyDataGridViewOfValueChange(); } protected virtual void NotifyDataGridViewOfValueChange() { valueChanged = true; if (dataGridView != null) { dataGridView.NotifyCurrentCellDirty(true); } } public Cursor EditingPanelCursor { get { return Cursors.IBeam; } } public DataGridView EditingControlDataGridView { get { return dataGridView; } set { dataGridView = value; } } public object EditingControlFormattedValue { set { if (value.ToString() != Text) { Text = value.ToString(); NotifyDataGridViewOfValueChange(); } } get { return Text; } } public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) { return Text; } public void PrepareEditingControlForEdit(bool selectAll) { } public bool RepositionEditingControlOnValueChange { get { return false; } } public int EditingControlRowIndex { get { return rowInd
If I was you, I'd think it would be easiest to get help if I posted the entire, actual error message, and only the little relevant piece of code. Otherwise, we have to guess where the error is coming from, and what it is, because no one is going to read through all that code to find where it is coming from.
I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
-
If I was you, I'd think it would be easiest to get help if I posted the entire, actual error message, and only the little relevant piece of code. Otherwise, we have to guess where the error is coming from, and what it is, because no one is going to read through all that code to find where it is coming from.
I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer
Justin, thank you. Exception occurs in Application.Run() method: System.InvalidOperationException was unhandled Message="Operation is not valid due to the current state of the object." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e) at System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender, ListChangedEventArgs e) at System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e) at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e) at System.ComponentModel.BindingList`1.OnListChanged(ListChangedEventArgs e) at System.ComponentModel.BindingList`1.InsertItem(Int32 index, T item) at System.Collections.ObjectModel.Collection`1.Add(T item) at System.ComponentModel.BindingList`1.AddNewCore() at System.ComponentModel.BindingList`1.System.ComponentModel.IBindingList.AddNew() at System.Windows.Forms.CurrencyManager.AddNew() at System.Windows.Forms.DataGridView.DataGridViewDataConnection.AddNew() at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnNewRowNeeded() at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred) at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) at System.Windows.Forms.DataGridView.ProcessDownKeyInternal(Keys keyData, Boolean& moved) at System.Windows.Forms.DataGridView.ProcessDownKey(Keys keyData) at System.Windows.Forms.DataGridView.ProcessDataGridViewKey(KeyEventArgs e) at System.Windows.Forms.DataGridView.ProcessKeyPreview(Message& m) ... This is probably caused because DataGridView does not call RemoveItem() event when it cancels edit. You can run this code to see it. Andrus.
Andrus