Combobox problem

Hi All,

I have a combobox and a text box. They are both databound to different fields of a datatable. The combobox contains a list of telephone number descriptions eg. Home, Mobile, Work. When an item is selected the appropriate number is displayed in the text box.

When I call AddNew the textbox and combobox clear as expected. The problem is that the description I type into the combobox is lost and replaced with a blank item in the combobox. Incidentaly, the blank entry when selected does correspond to the telephone number entered. No matter what I do I cant edit the text in the combobox!!

I sure I am missing a simple trick here,

Any ideas,

Danny

[664 byte] By [CodeScribler] at [2008-2-15]
# 1

ComboBox ComplexBinding is one-way (complex = using DataSource/DisplayMember/ValueMember). If you want to push values to the data source, you need to use simple binding (like you do for the TextBox).

The easiest way to do this is to have an AddNew dialog but this may not be ideal if expect a large number of numbers to be entered. If you don't want that, you'll need to do something a little more complex like hook the Validated event as shown below (and this may change based on how you cancel/commit the AddNew). Note that you don't need a binding either - you could just write the value to the data source in the Validated event.

Joe


Binding _b;
private void Form1_Load(object sender, EventArgs e)
{
_b = new Binding("Text", _yourDataSource, "Description", true, DataSourceUpdateMode.Never);
_b.ControlUpdateMode = ControlUpdateMode.Never;

this.descriptionComboBox.Validated += new EventHandler(descriptionComboBox_Validated);
this.descriptionComboBox.DataBindings.Add(_b);
}

private void descriptionComboBox_Validated(object sender, EventArgs e)
{
_b.WriteValue();
}

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Hi Danny,

could you give me the code how to chose a value / item / text in a combobox and then to display the corresponding value / text from another table in a listbox / datagrid, eg.?

I want to use a combobox as a filter, so e.g. you chose the month in a combobx and then, in the datagrid / listbox it should show all records from a table containing the month! Thank you!

Michael

MS82 at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...