ComboBox data binding - c#

I have a combobox control on form that pull its data (Displays and values) from some datasource. On another side I have table with one row. I want when app is lauching, combobox set selectedvalue or selecteditem to value of one column in above row. And when user has changed combobox it will persist change to row. I have tried to bind SelectedValue to this column, but it doesn't work. Combobox just sets on start to first item. What is problem?
EDIT
This is a Win Forms project.
Here is the binding code:
this.comboBoxCountries = new System.Windows.Forms.ComboBox();
this.countriesBindingSource = new System.Windows.Forms.BindingSource(this.components);
//
// comboBoxCountries
//
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.searchCriteriaBindingSource, "Postcode", true));
this.comboBoxCountries.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.searchCriteriaBindingSource, "CountryCode", true));
this.comboBoxCountries.DataSource = this.countriesBindingSource;
this.comboBoxCountries.DisplayMember = "Name";
this.comboBoxCountries.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxCountries.FormattingEnabled = true;
this.comboBoxCountries.Location = new System.Drawing.Point(190, 19);
this.comboBoxCountries.Name = "comboBoxCountries";
this.comboBoxCountries.Size = new System.Drawing.Size(156, 21);
this.comboBoxCountries.TabIndex = 2;
this.comboBoxCountries.ValueMember = "Code";
this.comboBoxCountries.SelectedValueChanged += new System.EventHandler(this.comboBoxCountries_SelectedValueChanged);
//
// countriesBindingSource
//
this.countriesBindingSource.DataMember = "Countries";
this.countriesBindingSource.DataSource = this.dbDataSetCountries;
//
// dbDataSetCountries
//
this.dbDataSetCountries.DataSetName = "dbDataSetCountries";
this.dbDataSetCountries.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
//
// searchCriteriaBindingSource
//
this.searchCriteriaBindingSource.AllowNew = false;
this.searchCriteriaBindingSource.DataMember = "SearchCriteria";
this.searchCriteriaBindingSource.DataSource = this.dbDataSetSearchCriteria;
this.searchCriteriaBindingSource.BindingComplete += new System.Windows.Forms.BindingCompleteEventHandler(this.searchCriteriaBindingSource_BindingComplete);
//
// dbDataSetSearchCriteria
//
this.dbDataSetSearchCriteria.DataSetName = "dbDataSetSearchCriteria";
this.dbDataSetSearchCriteria.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
EDIT2
As I have mentioned in my comment below, I have another textbox that is binded to another DataMember of same binding source and textbox working fine. It's appear with appropriate value. When I change DataMember on same datamember on which I set selectedvalue property of combobox binding it's also show a good result and work properly.
Thanks in advance!

Take a look at the DisplayMember and ValueMember properties of the combobox. You need to tell the ComboBox what member from the datasource to display in the drop down, and what value to give when SelectedValue is requested.
It sounds like your ComboBox is bound to a static list while your rows are not. You might consider using a BindingSource that you set the ComboBox and the DataGridView's DataSource to. That way when the DGV navigates to a new row, the ComboBox will be updated with the value for the new row.
Here is a link to the ComboBox on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

I find it out. So for managing with this issue you should remove SelectedValue databinding from visual studio data bound menu and put an appropriate code to add this databinding in some place after filling all bindingsources:
private void MainForm_Load_1(object sender, EventArgs e)
{
this.searchCriteriaTableAdapter1.Fill(this.dbDataSetCountries.SearchCriteria);
this.searchCriteriaTableAdapter.Fill(this.dbDataSetSearchCriteria.SearchCriteria);
comboBoxCountries.DataBindings.Add("SelectedValue", this.dbDataSetCountries.SearchCriteria, "CountryCode");
}

Related

Two ComboBox with same DataSource causes Selection to be forgotten

I have BindingSource defined:
public System.Windows.Forms.BindingSource bsContractors;
this.bsContractors.DataSource = typeof(Contractor);
and then a ComboBox with a DataSource defined like so:
private System.Windows.Forms.ComboBox cmbConstructionContractors1;
this.cmbConstructionContractors1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.bsProject, "Id", true));
this.cmbContractors1.DataSource = this.bsContractors;
this.cmbContractors1.DisplayMember = "Name";
this.cmbContractors1.ValueMember = "Id";
this.cmbContractors1.SelectedIndexChanged += new System.EventHandler(this.cmbContractor1Selected);
This works fine.
I have another ComboBox defined on another Form using the same DataSource:
this.cmbContractorName2.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", myView.bsProject, "Id", true));
this.cmbContractorName2.DataSource = projectView.bsContractors;
this.cmbContractorName2.ValueMember = "Id";
this.cmbContractorName2.DisplayMember = "Name";
this.cmbContractorName2.SelectedIndexChanged += new System.EventHandler(this.cmbContractor2Selected);
When this 2nd ComboBox is displayed, the first ComboBox, which has something selected, gets reset to the first entry, which is blank.
If I pull down on the first ComboBox, the list is still there, it just 'forgot' which one was selected.
Edit: I've discovered that when displaying the 2nd ComboBox, the EventHandler of the 1st ComboBox1 somehow gets assigned to cmbContractors2Selected instead of the original cmbContractors1Selected
Try giving it its own binding object:
this.cmbContractorName2.DataSource = new BindingSource(projectView.bsContractors, null);
This will separate the currency managers.

Selected index combobox winforms

Ok, very dumb question but i haven't really found an answer on internet.
I have multiple comboboxes on a form. The binding of each combobox is on form_load.
When the form loads, the first item is selected on the form. This is obvious, but I don't want this. So i used in the form_load the following code:
private void InvoiceView_Load(object sender, EventArgs e)
{
// Bind list of customers to combobox
CustomerComboBox.DataSource = invoicePresenter.getCustomers();
CustomerComboBox.DisplayMember = "CustomerName";
CustomerComboBox.ValueMember = "CustomerId";
// Bind list of products to combobox
productCombobox.DataSource = invoicePresenter.getProducts();
productCombobox.DisplayMember = "ProductName";
productCombobox.ValueMember = "ProductId";
// Bind list of vat codes to combobox
vatComboBox.DataSource = invoicePresenter.getTaxCodes();
vatComboBox.DisplayMember = "taxCodeShortDescr";
vatComboBox.ValueMember = "taxCodeId";
// Set comboboxes empty
CustomerComboBox.SelectedItem = null;
productCombobox.SelectedItem = null;
vatComboBox.SelectedItem = null;
}
This works. But the textboxes are still giving me the data of the first item ? My guess is because its in the selectedIndexChanged. But i have no clue what to use else.
If I put the combobox.selectedIndex = -1; I face the same issue.
the code:
private void CustomerComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Bind the selected customer itemvalues to the texboxes
txtCustomerName.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerName.ToString();
txtAddress.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerAddress.ToString();
txtPostalCode.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerPostalCode.ToString();
txtCity.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCity.ToString();
txtCountry.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCountry.ToString();
txtVatNumber.Text = ((tbl_customer)CustomerComboBox.SelectedItem).CustomerCountryCode.ToString() + ((tbl_customer)CustomerComboBox.SelectedItem).CustomerVat.ToString();
}
One would think, because im using selecteditem in the textbox binding, it would be null also. But this is not the case.
Interestingly, seems like you've hit one of the WF data binding quirks. The problem is caused by the fact that the CurrencyManager class which maintains every list data source does not allow setting the Position property to -1 (thus Current to null) when the list count is not zero. Since the ComboBox is synchronizing the SelectedIndex with the CurrencyManager.Position, this effectively prevents having an unselected item.
As a workaround, if the data bound mode of the list portion is not essential for you, replace the line
CustomerComboBox.DataSource = invoicePresenter.getCustomers();
with
foreach (var customer in invoicePresenter.getCustomers())
CustomerComboBox.Items.Add(customer);
Do the same for the other comboboxes that need such behavior.
You can remove the handler for the SelectedIndex_Changed event of combobox, bind data, then add the handler back. like this :
private void InvoiceView_Load(object sender, EventArgs e)
{
this.CustomerComboBox.SelectedIndexChanged -= new EventHandler(CustomerComboBox_SelectedIndexChanged);
this.productCombobox.SelectedIndexChanged -= new EventHandler(productCombobox_SelectedIndexChanged);
this.vatComboBox.SelectedIndexChanged -= new EventHandler(vatComboBox_SelectedIndexChanged);
// Bind list of customers to combobox
CustomerComboBox.DataSource = invoicePresenter.getCustomers();
CustomerComboBox.DisplayMember = "CustomerName";
CustomerComboBox.ValueMember = "CustomerId";
// Bind list of products to combobox
productCombobox.DataSource = invoicePresenter.getProducts();
productCombobox.DisplayMember = "ProductName";
productCombobox.ValueMember = "ProductId";
// Bind list of vat codes to combobox
vatComboBox.DataSource = invoicePresenter.getTaxCodes();
vatComboBox.DisplayMember = "taxCodeShortDescr";
vatComboBox.ValueMember = "taxCodeId";
this.CustomerComboBox.SelectedIndexChanged += new EventHandler(CustomerComboBox_SelectedIndexChanged);
this.productCombobox.SelectedIndexChanged += new EventHandler(productCombobox_SelectedIndexChanged);
this.vatComboBox.SelectedIndexChanged += new EventHandler(vatComboBox_SelectedIndexChanged);
}

Programmatically Set Data Binding in WPF for Dynamic TextBoxes from a Dynamic ComboBox

This part works.
In my C#.NET WPF XAML, I have a static ComboBox and a static TextBox. The TextBox displays another column from the same DataTable (in the ComboBox's ItemSource). The column "rr_code" is the column for company name and the column "rr_addr" is the column for the address.
<ComboBox x:Name="CompanyComboBox1" IsEditable="True" IsTextSearchEnabled="True" IsSynchronizedWithCurrentItem="False"/>
<TextBox x:Name="StreetTextBox1" DataContext="{Binding SelectedItem, ElementName=CompanyComboBox1}" Text="{Binding rr_addr}" IsManipulationEnabled="True"\>
The ComboBox reads programmatically from a column in a DataTable:
CompanyComboBox1.ItemsSource = Rails.DefaultView; // Rails is a DataTable
CompanyComboBox1.DisplayMemberPath = "rr_code"; // column name for company name
This part doesn't work
The question is, I now have a "Add Company" button that creates a new form in a StackPanel, dynamically and with this exact functionality. The ComboBox works exactly as expected. Here's what I have so far:
ComboBox companyComboBox = new ComboBox();
companyComboBox.ItemsSource = Rails.DefaultView;
companyComboBox.IsEditable = true;
companyComboBox.IsTextSearchEnabled = true;
companyComboBox.DisplayMemberPath = "rr_code";
The problem is in the TextBox, which is not updating when I change the dynamic companyComboBox, so I'm sure it has to do with the binding.
TextBox streetTextBox = new TextBox();
streetTextBox.DataContext = companyComboBox;
Binding b = new Binding("rr_addr");
b.Mode = BindingMode.Default;
b.Source = companyComboBox.SelectedItem;
streetTextBox.SetBinding(ComboBox.SelectedItemProperty, b);
What is the correct way to set the binding for the TextBox streetTextBox?
The code-behind equivalent of this particular XAML + C# data binding in pure C# is:
ComboBox companyComboBox = new ComboBox();
companyComboBox.ItemsSource = Rails.DefaultView; // Rails being DataTable
companyComboBox.IsEditable = true;
companyComboBox.IsTextSearchEnabled = true;
companyComboBox.DisplayMemberPath = "rr_code";
Binding b = new Binding("SelectedItem.rr_addr"); // The selected item's 'rr_addr' column ...
b.Source = companyComboBox; // ... of the companyComboBox ...
TextBox streetTextBox = new TextBox();
streetTextBox.SetBinding(TextBox.TextProperty,b); // ... is bound to streetTextBox's Text property.
The error was in the last line. SetBinding needed to have a property of the target, not the source. In addition, the Binding declaration needed "SelectedItem." for some reason.
Why are you setting the TextBox DataContext ?
You can simply bind TextBox.Text property to ComboBox SelectedItem in your XAML
<TextBox Text="{Binding ElementName=CompanyComboBox1, Path=SelectedItem.rr_addr}"></TextBox>

two combobox firing an event for same function

There are two comboboxes. one is created by drag&drop and set event SelectedIndexChanged. And 2nd combobox is created manually, but not given any event. But when I change 2nd combo, it's firing an event for 1st combo's function.
Form prompt = new Form();
prompt.Width = 300;
prompt.Height = 150;
ComboBox cmBox = new ComboBox() { Left = 70, Top = 24, Width = 100, Height=150 };
cmBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmBox.ValueMember = "value";
cmBox.DisplayMember = "text";
prompt.ShowDialog();
I tried, but not working:
cmBox.SelectedIndexChanged -= new System.EventHandler(comboBox1_SelectedIndexChanged);
P.S: They are using same bindsource.
If they're sharing a BindingSource, then changing the value in one control will change the value in the other control as well.
When you change the value in the second ComboBox, the value in the first ComboBox changes too, which fires its SelectedIndexChanged event.
Create a separate BindingSource for each control, or if it's a collection, try just assigning the collection directly to each ComboBox.

Set SelectedItem on a combobox bound to datasource

List<Customer> _customers = getCustomers().ToList();
BindingSource bsCustomers = new BindingSource();
bsCustomers.DataSource = _customers;
comboBox.DataSource = bsCustomers.DataSource;
comboBox.DisplayMember = "name";
comboBox.ValueMember = "id";
Now how do I set the combobox's Item to something other than the first in the list?
Tried
comboBox.SelectedItem = someCustomer;
...and lots of other stuff but no luck so far...
You should do
comboBox.SelectedValue = "valueToSelect";
or
comboBox.SelectedIndex = n;
or
comboBox.Items[n].Selected = true;
Your binding code is not complete. Try this:
BindingSource bsCustomers = new BindingSource();
bsCustomers.DataSource = _customers;
comboBox.DataBindings.Add(
new System.Windows.Forms.Binding("SelectedValue", bsCustomers, "id", true));
comboBox.DataSource = bsCustomers;
comboBox.DisplayMember = "name";
comboBox.ValueMember = "id";
In most cases you can accomplish this task in the designer, instead of doing it in code.
Start by adding a data source in the "Data Sources" window in Visual Studio. Open it from menu View > Other Windows > Data Sources. Add an Object data source of Customer type. In the Data Sources you will see the customer's properties. Through a right click on the properties you can change the default control associated to it.
Now you can simply drag a property from the Data Sources window to you form. Visual Studio automatically adds A BindingSource and a BindingNavigator component to your form when you drop the first control. The BindingNavigator is optional and you can safely remove it, if you don't need it. Visual Studio also does all the wire-up. You can tweak it through the properties window. Sometimes this is required for combo boxes.
There's only one thing left to do in your code: Assign an actual data source to the binding source:
customerBindingSource.DataSource = _customers;
this works for me
bsCustomers.Position = comboBox.Items.IndexOf(targetCustomer);

Categories