G
gsa
Scenario:
Two classes (objA and objX), the class objX has a field of type objA.
class objA {
private double _a;
public double a {
get { return _a; }
set { _a = value; }
}
private int _b;
public int b {
get { return _b; }
set { _b = value; }
}
public override string ToString() {return _a.ToString();}
}
class objX {
private objA _objA;
public objA A {
get { return _objA; }
set { _objA = value; }
}
//...
}
Several instances of both objects.
objA a = new objA(); objA b = new objA(); objA c = new objA();
a.a = 1.1; a.b = 1;
b.a = 2.2; b.b = 2;
c.a = 3.3; c.b = 3;
objX x = new objX(); objX y = new objX(); objX z = new objX();
x.A = a;
y.A = b;
z.A = c;
A DataGridView with a ComboBox column showing the list of objX objects,
and the combobox(es) having the objAs in the list using
BindingSource(s) to populate everything. Like this:
comboBS.Add(a);
comboBS.Add(b);
comboBS.Add(c);
gridBS.Add(x);
gridBS.Add(y);
gridBS.Add(z);
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.DataSource = this.comboBS;
cell.Value = typeof(objA);
cell.ValueType = typeof(objA);
dataGridView1.Columns[0].CellTemplate = cell;
where comboBS and gridBS are BindingSource(s) created within the Visual
Sudio IDE.
Now, everything works fine and the grid appears on the screen, showing
correct values in each cell.
Trying to change one of the combobox values, however, raises an
exception in the DataGridViiew of type System.FormatException (Invalid
cast from System.String to objA).
Note that no field in both object is of type String, so there must be
some internal representation wich the grid uses and then tries to cast.
I googoled for days but found only samples and articles showing
DataGridView using database tables as data, NOT objects.
Thank You for any help,
gsa
Two classes (objA and objX), the class objX has a field of type objA.
class objA {
private double _a;
public double a {
get { return _a; }
set { _a = value; }
}
private int _b;
public int b {
get { return _b; }
set { _b = value; }
}
public override string ToString() {return _a.ToString();}
}
class objX {
private objA _objA;
public objA A {
get { return _objA; }
set { _objA = value; }
}
//...
}
Several instances of both objects.
objA a = new objA(); objA b = new objA(); objA c = new objA();
a.a = 1.1; a.b = 1;
b.a = 2.2; b.b = 2;
c.a = 3.3; c.b = 3;
objX x = new objX(); objX y = new objX(); objX z = new objX();
x.A = a;
y.A = b;
z.A = c;
A DataGridView with a ComboBox column showing the list of objX objects,
and the combobox(es) having the objAs in the list using
BindingSource(s) to populate everything. Like this:
comboBS.Add(a);
comboBS.Add(b);
comboBS.Add(c);
gridBS.Add(x);
gridBS.Add(y);
gridBS.Add(z);
DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
cell.DataSource = this.comboBS;
cell.Value = typeof(objA);
cell.ValueType = typeof(objA);
dataGridView1.Columns[0].CellTemplate = cell;
where comboBS and gridBS are BindingSource(s) created within the Visual
Sudio IDE.
Now, everything works fine and the grid appears on the screen, showing
correct values in each cell.
Trying to change one of the combobox values, however, raises an
exception in the DataGridViiew of type System.FormatException (Invalid
cast from System.String to objA).
Note that no field in both object is of type String, so there must be
some internal representation wich the grid uses and then tries to cast.
I googoled for days but found only samples and articles showing
DataGridView using database tables as data, NOT objects.
Thank You for any help,
gsa