Andrus

Andrus

  • NA
  • 3
  • 0

ComboBoxColumn in DataGridView causes invalid operatoion exception

Apr 18 2008 12:20 PM

Run code, press down arrow, up arrow, down arrow.
Exception occurs in Application.Run() line. 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 rowIndex;
      }

      set {
        rowIndex = value;
      }
    }

    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle
    dataGridViewCellStyle) {
      DropDownStyle = ComboBoxStyle.DropDown;
    }

    public bool EditingControlWantsInputKey(Keys keyData, bool
    dataGridViewWantsInputKey) {
      return !dataGridViewWantsInputKey;
    }

    public bool EditingControlValueChanged {

      get {
        return valueChanged;
      }
      set {
        valueChanged = value;
      }
    }
  }
}


Answers (1)