Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Combobox in DataGridView causes invalid operation except
Message
From
20/04/2008 00:44:21
 
 
To
18/04/2008 11:32:39
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01311696
Message ID:
01311922
Views:
15
Andrus,

I can't get your code to compile. Can you post a correction?

~~Bonnie



>Run code, press down arrow, up arrow, down arrow.
>InvalidOperatonException occurs in Application.Run()
>
>How to fix ?
>
>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(new DataGridViewComboBoxColumn());
> grid.Columns.Add(comboBoxColumn);
> BindingList l = new BindingList();
> 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;
> }
> }
> }
>}
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform