Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Combox in datagridview causes exception

Combox in datagridview causes exception

Scheduled Pinned Locked Moved C#
csshelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndrusM
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • A AndrusM

      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

      J Offline
      J Offline
      Justin Perez
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • J Justin Perez

        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

        A Offline
        A Offline
        AndrusM
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups