Groups | Blog | Home
all groups > dotnet windows forms databinding > january 2005 >

dotnet windows forms databinding : Binding enum to ComboBox


kh
1/14/2005 2:07:01 AM
Hi. I have a type that I am binding to a form. One of the properties is an
enum and I want the user to be able to select the value from a ComboBox. I am
trying to do this using inbuilt data binding rather than coding up the
plumbing to change the property value myself. Basically, I want to bind to
the ComboBox as follows:

myCombo.DataBindings.Add("SelectedValue", myObject, "MyEnumProperty")

I have not yet been able to populate the ComboBox such that the above is
possible. How can I do this?

Thanks

kh
kh
1/14/2005 2:53:03 AM
A quick update. I have been able to get the ComboBox to display the current
value of the enum property for my object, but if I change the value via the
ComboBox I can succesfully trace the code through the property setter and see
my property's change event fired (as required for bound properties and change
notification) but I am not able to move focus away from the ComboBox. Its as
if the value was invalid for some reason. I have to kill the app to continue.
Any ideas?

Thanks again

kh

[quoted text, click to view]
v-kevy NO[at]SPAM online.microsoft.com
1/15/2005 6:02:37 AM
Hi kh,

We have reviewed this issue and are currently researching on it. We will
update you ASAP. Thanks for your patience!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
v-jetan NO[at]SPAM online.microsoft.com (
1/17/2005 2:59:23 AM
Hi,

Sorry for letting you wait for so long. Based on my understanding, in your
custom object colleciton each object has a property which is enum type, you
have managed to display the enum names in the combobox, but you failed to
modify it.

For combobox, it will not allow us to modify certain item in the bound
collection, it just used for select certain item in the collection.
I have used the following code snippet to do a test:

public enum CustomColor
{
white=0,
red=1,
yellow=2,
blue=3
};

public class TestClass
{
private CustomColor _testprop=CustomColor.white;
public CustomColor testprop
{
get
{
return _testprop;
}
set
{
_testprop=value;
}
}
}

TestClass [] arr;
private void button1_Click(object sender, System.EventArgs e)
{
arr=new TestClass[4];
for(int i=0;i<4;i++)
{
arr[i]=new TestClass();
arr[i].testprop=(CustomColor)Enum.Parse(typeof(CustomColor),
Enum.GetName(typeof(CustomColor), i));
}
this.comboBox1.DataSource=arr;
this.comboBox1.ValueMember="testprop";
this.comboBox1.DisplayMember="testprop";
}

private void button2_Click(object sender, System.EventArgs e)
{
for(int i=0;i<4;i++)
{
Console.WriteLine(this.arr[i].testprop.ToString());
}
}

If we select item with text "white" in the dropdownlist, then if we modify
the text "white" as "red", combobox will not modify the current bound
item's value to "red"'s value, but just change the current bound item index
to first item with text "red". This is combobox's default behavior.
Combobox is used to select certain item in a collection, not for modifying
an item's property.

For modifying issue, I think you may refer to other winform controls, such
as textbox or datagrid etc..
If I misunderstand you, please feel free to tell me, thanks.
============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
kh
1/17/2005 4:41:02 AM
Jeffrey/Kevin

Actually, I don't want the user to edit the text in the combo, just use it
to select the value for the property. I was getting nowhere with my first
implementation so I started again and got it working perfectly. Turns out my
property setter was not completing because an exception was being thrown and
swallowed as the update propogated through the code. This was preventing
focus from moving away from the combo.

Many thanks for you help

kh
v-jetan NO[at]SPAM online.microsoft.com (
1/18/2005 6:44:55 AM
Hi kh,

Thanks for your feedback!!

I am glad my reply makes sense to you. Do you still need help on this
issue? Please feel free to tell us, we will be standing by to be of
assistance. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
AddThis Social Bookmark Button