Hi,
I have a problem with the binding of a custom class with custom class object within it.
I have a class Code which contain a class CodeType:public class Code{ public int ID { get; set; } public string Name { get; set; } public string Description { get; set; } public CodeType CodeTypeName { get; set; }}public class CodeType{ public int Id { get; set; } public string Name { get; set; }}
When I try to load the form with the following code:public partial class Form1 : Form { private Infragistics.Win.ValueList vl = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { List<CodeType> ct = new List<CodeType>(); ct.Add(new CodeType() { Id = 1, Name = "one" }); ct.Add(new CodeType() { Id = 2, Name = "two" }); List<Code> c = new List<Code>(); c.Add(new Code() { ID = 1, Name = "test", Description = "test", CodeTypeName = ct[0] }); c.Add(new Code() { ID = 2, Name = "test", Description = "test", CodeTypeName = ct[1] }); c.Add(new Code() { ID = 3, Name = "test", Description = "test", CodeTypeName = new CodeType() { Id = 1, Name = "one" } }); c.Add(new Code() { ID = 4, Name = "test", Description = "test", CodeTypeName = new CodeType() { Id = 2, Name = "two" } }); vl = new Infragistics.Win.ValueList(); vl.ValueListItems.Add(ct[0], ct[0].Name); vl.ValueListItems.Add(ct[1], ct[1].Name); this.ultraGrid1.DataSource = c; } private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { e.Layout.Bands[0].Columns["CodeTypeName"].ValueList = this.vl; e.Layout.Bands[0].Columns["CodeTypeName"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDownList; } }
I get invalid entry in the last two rows because even though the properties are the same. I believe the object itself is different because of it signature. Is there a way to prevent that from happening?
Is there a way to prevent that from happening?
I do not wish to add a ToString() or Equals() method override in my CodeType class.
Thank you
Brian Fallon"] The alternative is to use some other value for the items in the ValueList (for example, the value CodeTypeName's Id property). You would then have to incorporate some kind of logic that gets you from the ID to a CodeTypeName instance, if you need to reference these actual instances.
The alternative is to use some other value for the items in the ValueList (for example, the value CodeTypeName's Id property). You would then have to incorporate some kind of logic that gets you from the ID to a CodeTypeName instance, if you need to reference these actual instances.
I was under the impression from your previous response that this was the way to go if I wanted to be able to use the ValueList.
Could you show an example on how to associate similar object but with different instances using the ValueList?
Sorry, it is hard to speculate about a workaround without a better handle on exactly what you are trying to achieve. One thing I'm not grasping is why it is important to assign the CodeTypeName property of the Code object with a specific instance, i.e., from the perspective of the data, the object identity of the value should not be important.
If for the sake of argument it is important to be able to associate your Code object with a specific instance of these CodeTypeName objects, you could store these instances in a dictionary, keyed off the ID, and make the DataValue in the ValueList the ID instead of the actual instance. Then, when you need to associate with the actual instances, use the cell's value to key into the dictionary. This will ensure that each row uses one of these known instances.
Ok
I just wanted to be sure I was seeing and thinking clearly.
Would adding some logic inside the InitializeRow event to retreive the proper instances be a good workaround? I am thinking of manually comparing two CodeType (with respect to their properties) and then reassign the cell value with the same CodeType instance already loaded in the ValueList.
I think you answered your own question...the values you used for the ValueList's items are actual instance references to your CodeTypeName object, and the first two rows in the grid have these instance references assigned to their CodeTypeName property. Since the values assigned for the first two rows match by virtue of referencing the same instance, the ValueList successfully "links" them up. The last two rows, however, have differenent instances assigned to their CodeTypeName property, thus do not match the items in the ValueList.
The only way around this is to override the Equals method, which you stated you didn't want to do (I don't think that is a good idea either. The alternative is to use some other value for the items in the ValueList (for example, the value CodeTypeName's Id property). You would then have to incorporate some kind of logic that gets you from the ID to a CodeTypeName instance, if you need to reference these actual instances.
No problem!
Here is the sample solution.