Hi,
I have an object named "Item" and i bind it with ultra combo. But i cannot set the SelectedItem for it like we do in .Net native combo by using SelectedItem property, so that when it fetches records from database, the selected item automatically sets.. Instead, here in case of ultra combo, i have set the value property of ultra combo with my Item.Key (which is the primary key) in coding.. Also at the time of saving, i have to set the Item object with Ultracombo.SelectedRow.ListObject... I think this is the overhead in ultra combo.. Since .Net native combo needs not to set these properties in coding...
Your help required...
Ovais.
If you want the UltraCombo's value to be associated with the actual ListObject of the row, rather than a particular field in that row, you can do this:
this.ultraCombo1.ValueMember = Infragistics.Win.BindableValueList.USE_LISTOBJECT_AS_VALUEMEMBER
Thanks Mike.
But i dont want to write any code for combo binding.. Is there any alternate?
Hi Mike,
I found this post how to specify the ValueMenber for binding objects. I can choose a object from the dropdown list of my UltraComboEditor (UltraCombo seems not to work for this) and the property of my business object "SumProfile" gets updated with the choosen value. My drop down object (class UserGroup) overrides the ToString() member and returns the "GroupName".
Problem: the next time I call my form the combo appears empty (objects are loaded ok from database). I have setup my combo editor as follows:
Form_load event:
BindingList<SumProfile> profiles = InitBindingList;
this.bindingSourceProfiles.DataSource = profiles;
List<UserGroup> list = LoadUserGoups();
this.ComboGrantedUserGroup.DropDownStyle = DropDownStyle.DropDownList;
this.ComboGrantedUserGroup.ValueMember = Infragistics.Win.BindableValueList.USE_LISTOBJECT_AS_VALUEMEMBER;this.ComboGrantedUserGroup.DisplayMember = "GroupName"; //string.Empty;this.ComboGrantedUserGroup.DataSource = list;
this.ComboGrantedUserGroup.DataBindings.Add( "Value", this.bindingSourceProfiles, "GrantedUserGroup" );
Any ideas why the combo editor dont't displays the "GrantedUserGroup" - property?
Best Regards
Markus
Hi Markus,
My guess is that when you load the form, the Value of the combo is being set by the the data source to an object. That object might be the same as the object on the list, but the Equals method on an object returns true only if the objects are the same instance - and in this case, they are two different instances of the object.
In other words, even if the object returned from the DataBase has the same property values as the one on the list, they are not the same object and the Equals method will return false.
So when you click an item on the list, the Combo's value is set to the value on the list - it's the same object. But when you load the app, the Value of the combo is set by the data source to a new instance of the object and it's not the same one as the instance on the list. They don't match, so the combo doesn't know the Value matches an item on the list. You probably have your combo DropDownStyle set to DropDownList, which means the combo will not accept a value that doesn't exist on the list and so it shows blank.
One way to get around this would be to override the Equals method on your object so that it returns true when all of the properties of the objects are equal. If you do that, you should also override GetHashCode to make sure they return the same value.
Frankly, though, I'm not sure this is a great idea. Having an object that returns Equals for other instances can cause some weird issues when using DataBinding. If I were you, I would probably not bind the Value property of the combo and simply set the Value in code to an instance that is on the list.
thats the point! In the form load I load the user-groups now first and use this list to load and instantiates the profiles and also for binding to the combos data source.
How binding object properties is hard to find in the help and forum posts. It is somewhat of "hidden". This scenario looks very common to me if you work with business objects. I guess I can do this the same way for enum-type properties? This is also a very common scenario in my project. So far I have implemented view-classes or have exposed enum-value-int property just for binding.
Thank you Mike.
Best regards
Yeah, I can understand how DataBinding might be confusing and arcane in some ways.
I'm not sure I understand exactly what you mean about enum-type properties. An Enum would not have the same problem because enums are value types and therefore the Equals operator on an enum will return true for the same value regardless of the instance. It's only object types that do an instance comparison.
Using an object as a data value is kind've unusual, in my experience. Most people use a primary key value, which is usually numeric.
In my scenarios it is very common to have a objects not only exposing properties of basic types. Fully understanding how to bind object and enum properties prevents me often implementing view classes. For persistent objects I have an ID (numeric) and it would be possible to bind that way you mentioned. For other objects I can not. I just like to understand the scenarios to choose the best:
public class SumProfile {
// Enum Property
public CalcModes { get { return mode;} set { mode= value} }
// Object property
public UserGroup { get { return group;} set { group= value} }
}
We have discussed the object property so far and I know how it works now. My question was if the enum property works the same way. If I bind the enum property I always get a string value and not the enum (int) value. Therefore the value does not get updated in my object. For these properties I exposes a enumValue property sometimes but I don't like to have such properties in my business objects. What is the best way to bind (standalone combo) enum values?
I hope you understand my question (sorry, its not my home language).
Well... if you have a Datatable with the Enum Values and the Display Text you want, what you would do is set up the combo using the ValueMember and DisplayMember. The ValueMember is the column in the list that contains the actual enum value. The DisplayMember is what you want the user to see - the friendly localized) text. The Combo will take care of showing the DisplayMember and the Value property of the combo will return the matching Enum value. You would bind the Value property of the combo to whatever field that you want to select an enum value into.
I'm not sure why you need an enumIntValue - unless maybe your back end data source doesn't support enums. But I would think the enum would get stored as an Int, anyway. That part is a little outside my area of expertise, as it's really nothing to do with the grid. But I imagine the DotNet framework would handle converting the enum into an Int and vice versa when it gets and sets the data on the DataTable.
I know that enums are value types. I just did not found a sample so far what is the best approach to bind enum value properties. In most cases I have to localize the enums that users can choose user friendly text in the current UI culture. For this I create small DataTable with two columns (enumIntValue, localized enum text). My object has a property of enum type. For binding I need extra proeprty in my object exposing a EnumIntValue-property. In the set member of this property I convert the 'value' to the enum-value assigned to objects data field.
Do you know a better approach? Without enumIntValue-property?
No... enums do not work the same way as objects. Enums are essentially integers. So 15 = 15 and there's no such thing as having a different instance of the number 15 so there's no ambiguity in the Equals operator.
If you are getting string, then something in your code is converting to, or looking, the ToString of the enum instead of the actual e-mail value.