I want to fill combobox content with my list in WPF. I can do it normally in winform but wpf looks a bit different..
I dont create any code in XAML. whole controls create dynamically and at run time..
so here is my code
cmbKriterKategori is a combobox.
cmbKriterKategori.DisplayMemberPath = "Value";
cmbKriterKategori.SelectedValuePath = "Key";
cmbKriterKategori.ItemsSource = yHelper.GetCriterList().ToList();
an error occours
Items collection must be empty before using ItemsSource.
and I also tried like that
cmbKriterKategori.DisplayMemberPath = "Value";
cmbKriterKategori.SelectedValuePath = "Key";
cmbKriterKategori.DataContext = yHelper.GetCriterList().ToList();
it doesnt occour any error but combobox hasnt any item..
yHelper.GetCriterList().ToList(); this function returns List>
and yHelper.GetCriterList() returns Dictionary
I used that code in winform and it works..
cmbKriterKategori.DisplayMember = "Value";
cmbKriterKategori.ValueMember ="Key";
cmbKriterKategori.DataSource = yhelper.GetCriterList().ToList();
So, What is the problem?
you have to clear your combo items so the Items collection must be empty before using ItemsSource exception won't be thrown
cmbKriterKategori.Items.Clear();
cmbKriterKategori.DisplayMemberPath = "Value";
cmbKriterKategori.SelectedValuePath = "Key";
cmbKriterKategori.ItemsSource = yHelper.GetCriterList().ToList();
Related
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.
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);
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");
}
I have a ComboBox that is binded to a DataSet. I wanted to show the selected value whenever the ComboBox has a change in selection. I have the following code:
private void devCb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
dv = new DataView(
dt,
"Device_ID = " + devCb1.SelectedIndex,
"Data_ID ASC",
DataViewRowState.CurrentRows);
dataDg1.ItemsSource = dv;
devCb1.DisplayMemberPath = "Content";
MessageBox.Show(devCb1.SelectedValue.ToString());
}
But it only gives me
System.Data.DataRowView
I have already set the DisplayMemberPath, but it still not showing me the selected item's content. What is wrong?
[EDITED]
I also tries the following
devCb1.SelectedValuePath = "Content";
MessageBox.Show(devCb1.SelectedValue.ToString());
But it also still gives me
System.Data.DataRowView
Doesn't work either..
DataRowView has no content from what i can see. Your SelectedValuePath should point to Row i think (or if the current path correctly points to the Content of the ComboBoxItem you just need to cast at that point), and you'd still need to cast the SelectedValue being a row, to that class and from there you can get some of its content.
Set breakpoints, use the debugger, look at the data in your objects.
Try SelectedValuePath to access the data you want to view
Well it is possible to access the data within DataRowView at least now (2 years after the question was made), by simply casting the SelectedValue to System.Data.DataRowView and accessing property Row["colName"] like this:
((System.Data.DataRowView)ComboBoxName.SelectedValue).Row["colName"];
I have a DropDownList which I initialize in a function called CreateChildControls before adding it to the Controls collections. I then override the Render() method and then render the DropDownList. The web part inherits from System.Web.UI.WebControls.WebParts.WebPart.
I bind the DropDownList in my web part like this:
private void BindClientTypes()
{
DataTable dt = DB.GetAllClientTypes();
if (dt == null)
{
ltGlobalErrorMsg.Text = GlobalErrorMessage;
ltGlobalErrorMsg.Visible = true;
}
else
{
ddlClient.DataSource = dt;
ddlClient.DataValueField = "ID";
ddlClient.DataTextField = "Name";
ddlClient.DataBind();
ddlClient.Items.Insert(0, PleaseSelectItem);
}
}
If I try to set the SelectedIndex of the DropDownList after calling DataBind, I get an error which says the control cannot have multiple selected items.
That code works fine, and I can set the SelectedIndex after the data bind if I comment out this line:
ddlClient.Items.Insert(0, PleaseSelectItem);
Can anyone explain why this wouldn't work?
Thanks.
ddl.Items.Add(new ListItem("yourtext", "yourvalue"));
When you set the 'selected' property you are setting it to that ListItem's instance so if you have more ListItems that you are reusing then they will all get the same value which is probably causing the issue you are experiencing.
To illustrate the problem see this example with 2 dropdownlists:
ListItem item1 = new ListItem("1", "1");
ListItem item2 = new ListItem("2", "2");
ListItem item3 = new ListItem("3", "3");
ddlTest.Items.Add(item1);
ddlTest.Items.Add(item2);
ddlTest.Items.Add(item3);
ddlTest2.Items.Add(item1);
ddlTest2.Items.Add(item2);
ddlTest2.Items.Add(item3);
ddlTest2.SelectedValue = "2";
Setting ddlTest2's selected value actually sets ddlTest as well since they share the same items list. If you run this bother ddlTest and ddlTest2 will have the exact same selected value even though only ddlTest2 was set.
Where is PleaseSelectItem declared? If you add the same instance of a listitem to many dropdownlists you are liable to get this problem.