Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to clear a combobox?
Message
From
07/05/2004 14:29:54
 
 
To
07/05/2004 14:17:58
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00898211
Message ID:
00902016
Views:
45
Lee,

> ComboBox.SelectedText has the data from the DisplayMember field and ComboBox.SelectedValue has the associated data from the ValueMember field

Yep, that's what happens. But that only does you any good if you then *do* something with those. That's where DataBinding comes in.

Does .DataBindings do something in addition to this? I'm confused on what exactly .DataBindings do

Say you have a form where you edit data from a table. One column in your table can be one of several different codes. Here's where you would use a Combo to represent that column in your table. The Combo's DataSource would be a different table (a code table) containing all the possible codes for that particular column in your main table. When you choose an item from that Combo, it won't automatically get saved to the column in your main table unless you DataBind it. If you don't DataBind it, you would have to manually set the value of the column in your main table (in either the Combo's SelectedValueChanged, SelectedIndexChanged or Validated event handlers). By DataBinding to the column, it's all done automatically ... which is obviously the preferred way of doing things.

~~Bonnie



>Bonnie,
>
>Ok, that's a little clearer... I'm still a little confused about the two kinds of binding. Just setting DataSource, DisplayMember, and ValueMember has worked for me (sometimes) before. The ComboBox displays the data from the DisplayMember field in the table, and when SelectedIndexChanged fires, the ComboBox.SelectedText has the data from the DisplayMember field and ComboBox.SelectedValue has the associated data from the ValueMember field. Does .DataBindings do something in addition to this? I'm confused on what exactly .DataBindings does.
>
>-----
>Lee Perkins
>
>>Lee,
>>
>>Setting the DataSource, ValueMember, etc. is setting up the stuff for populating the Combo with your codes tables or whatever you're using. Setting up the .DataBinding tells the combo which column in your data table contains the value that the Combo should display. Combo's should never be bound to SelectedText ... only to SelectedValue. That's just what I've found to work best (if your DisplayMember and ValueMember are the same, then it will probably work ok to bind to SelectedText, but if you make it a point to always bind to SelectedValue, then you won't have any possible gotcha's showing up later <g>).
>>
>>Combos are different than TextBoxes in that they have two kinds of "binding" ... one to populate the combo's dropdown and one to bind to your data table.
>>
>>~~Bonnie
>>
>>
>>>Bonnie,
>>>
>>> I'm just learning this .NET stuff (and probably always will be) and I was curious why you would call cboCode.DataBindings.Add(...) after setting the ValueMember property. The only reason I ask is because I've been having a lot of trouble lately with getting the SelectedValue member to reflect the value of the selected item. I do exactly as you did below, only without calling the DataBindings method. I thought setting the ValueMember property to the name of the field you wanted to bind it to would work. I've found that sometimes it does, and sometimes it doesn't. Should I be calling Databindings? And if so, why only for the SelectedValue, and not for SelectedText?
>>>
>>>-----
>>>Lee Perkins
>>>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform