Joe
2008-08-06 12:51:25 UTC
I have an object that inherits from BindingList<T> that I'm using as a data
source for a DataGridView. The items in the BindingList have 2 properties
which are enums. I'm using the CellParsing to assign e.Value to my enum
value and in the CellFormatting I'm doing the reverse. The problem is that
the CellFormatting e.Value is always the 1st value in the ComboBox list and
not the real value in the underlying object.
Also, dataGridView1_DataError always gets called with e.Exception =
"DataGridViewComboBoxCell value is not valid." and the Context = Formatting
| Display.
Any ideas?
For example:
enum MyEnum {Item1, Item2, Item3};
public MyClass
{
private MyEnum myenum;
private string astring;
public MyEnum TheEnum
{
get {return myenum;}
set {myenum = value;}
}
public string MyString
{
get {return astring;}
set {astring = value;}
}
}
public class MyListClass : BindingList<MyClass>
{
}
.....
DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn();
column1.Items.AddRange("First Item", "Second Item", "Third Item");
dataGridView1.Columns.Add(column1);
......
MyListClass listClass = new MyListClass();
dataGridView1.DataSource = listClass;
....
private void dataGridView1_CellParsing(object sender,
DataGridViewCellParsingEventArgs e)
{
if (e.DesiredType == typeof(MyEnum))
{
if (e.Value != null)
{
switch (e.Value.ToString())
{
case "First Item": e.Value = MyEnum.Item1; break;
case "Second Item": e.Value = MyEnum.Item2; break;
case "Third Item": e.Value = MyEnum.Item3; break;
}
e.ParsingApplied = true;
}
}
}
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
// Here e.Value always equals "First Item" even after a
different item has been selected.
switch (e.ColumnIndex)
{
case 0:
if (e.Value != null && e.Value.GetType() ==
typeof(MyEnum))
{
switch ((ColumnDataFilterOperator)e.Value)
{
case MyEnum.Item1: e.Value = "First Item";
break;
case MyEnum.Item2: e.Value = "Second Item";
break;
case MyEnum.Item3: e.Value = "Third Item";
break;
}
}
}
}
Thanks for any help,
Joe
source for a DataGridView. The items in the BindingList have 2 properties
which are enums. I'm using the CellParsing to assign e.Value to my enum
value and in the CellFormatting I'm doing the reverse. The problem is that
the CellFormatting e.Value is always the 1st value in the ComboBox list and
not the real value in the underlying object.
Also, dataGridView1_DataError always gets called with e.Exception =
"DataGridViewComboBoxCell value is not valid." and the Context = Formatting
| Display.
Any ideas?
For example:
enum MyEnum {Item1, Item2, Item3};
public MyClass
{
private MyEnum myenum;
private string astring;
public MyEnum TheEnum
{
get {return myenum;}
set {myenum = value;}
}
public string MyString
{
get {return astring;}
set {astring = value;}
}
}
public class MyListClass : BindingList<MyClass>
{
}
.....
DataGridViewComboBoxColumn column1 = new DataGridViewComboBoxColumn();
column1.Items.AddRange("First Item", "Second Item", "Third Item");
dataGridView1.Columns.Add(column1);
......
MyListClass listClass = new MyListClass();
dataGridView1.DataSource = listClass;
....
private void dataGridView1_CellParsing(object sender,
DataGridViewCellParsingEventArgs e)
{
if (e.DesiredType == typeof(MyEnum))
{
if (e.Value != null)
{
switch (e.Value.ToString())
{
case "First Item": e.Value = MyEnum.Item1; break;
case "Second Item": e.Value = MyEnum.Item2; break;
case "Third Item": e.Value = MyEnum.Item3; break;
}
e.ParsingApplied = true;
}
}
}
private void dataGridView1_CellFormatting(object sender,
DataGridViewCellFormattingEventArgs e)
{
// Here e.Value always equals "First Item" even after a
different item has been selected.
switch (e.ColumnIndex)
{
case 0:
if (e.Value != null && e.Value.GetType() ==
typeof(MyEnum))
{
switch ((ColumnDataFilterOperator)e.Value)
{
case MyEnum.Item1: e.Value = "First Item";
break;
case MyEnum.Item2: e.Value = "Second Item";
break;
case MyEnum.Item3: e.Value = "Third Item";
break;
}
}
}
}
Thanks for any help,
Joe