c# - Datagridview combobox cell which accept enum as integer -


I tried to override the default behavior of datagridview combo box so that it can be accepted Ancom as int. To do this, I CustomComboboxCell and CustomComboboxEditingControl :

  public class CustomComboboxEditingControl: DataGridViewComboboxEditingControl {public override object EditingControlFormattedValue {get {return base.EditingControlFormmatedValue; } Set {if (value.GetType (.isEnum)} {// enum to int base.EditingControlFormattedValue = (int) value; } And {base.EditingControlFormattedValue = value; }}}} Public class CustomComboboxCell: DatagridviewComboboxCell {public override type editType {return typeof (CustomComboboxEditingControl); }}   

Then in my form, I create a datagrid view with a comb-box column to test it:

  public enum TestEnum {A = 1, b = 2, c = 3,} Public class test {public testname {get; Set;}} Public Class Form 1 (Public Form 1) {Data Gridviewview DGV = New Data Gridview (); Dgv.AutoGenerateColumns = false; Datagridiv combobox column call 1 = new datagravid comboBok column (); Col1.CellTemplate = New CustomComboboxCell (); // Set data source for col1 dictionary & lt; Int, string & gt; DCT = new dictionary & lt; Int, string & gt; (); Dct.Add (1, "a"); Dct.Add (2, "B"); Dct.Add (3, "c"); Col1.Datasource = New binding resource () {data source = DCT}; Col1.ValueMember = "key"; Col1.DisplayMember = "Value"; col1.DataPropertyName = "test"; dgv.Columns.Add (col1); dgv.Invaidate (); this.Constrols.Add (DGV); // Add Data Source to the Data Grid View List & gt; Test & gt; Lst = new list & lt; Test & gt; (); Lst.Add (new test () {test = TestEnum.a}); Lst.Add (new test () {test = TestEnum.b}); Lst.Add (new test () {test = TestEnum.c}); Dgv.Datasource = New binding list () {data source = lst}; }}   

Iogg that everything looks right, but when I run the form, it's still not acceptable to me system.ArgumentException: DataGridViewComboboxCell value.

Please take a look at your code and tell me what I remember?

After struggling for one night, finally I finally decided it with an "ugly" solution I did not want to do that from the beginning. I dictionary to the dictionary & lt; Testname, string & gt; Switch from using as a data source

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -