I am working on a project for my college where I need to bind data from database into the combobox. I need to store the roll no / enrollment no in the "value" field of combobox and name of the student in the "text" property of the combobox. How can I do that?>???
Please reply ASAP....
You will need to set the DataSource of the combobox to your datasource. Then the ValueMember for the Roll No and the DisplayMember for the name of the student.
e.g
cboStudents.DataSource = dataSet1.Tables["Students"];
cboStudents.ValueMember = "RollNumber";
cboStudents.DisplayMember = "StudentName";
The two complex-bound controls you've most likely encountered are the ComboBox and the Listbox. To complex-bind one of these controls, you need to set the DataSource (where values originate), the DisplayMember (the name of the column of data that supplies the visible list items), and the ValueMember (the name of the column of data that supplies the possible control values).
combobox.DataSource = dataTable
combobox.ValueMember = "id"
combobox.DisplayMember = "name"
Related
Forgive me if this is a rather simple question but I can't figure it out.
I have a frontend build in WPF.
It has a combobox with a datasource from a localDB.
The comboBox has a DisplayMember that gets filled at runtime.
I also have a text label and I want its text property to be whatever the displaymember is at runtime. The displaymember value is an object the user selects via the comboBox dropdown list, for example shoes.
comboBox.DataSource = ProductLogic.GetProduct();
comboBox.DisplayMember = "ProductName";
If I call it like this:
label.Text = comboBox.DisplayMember
It gets the value at code-time (which is "ProductName", but I need i the text value to be whatever it is at runtime. For example "Shoes". How would I go about this?
DisplayMember holds the name of the property which value is displayed in combobox (or any other control which supports it). So the value of DisplayMember is not changing. You probably want to use Text property.
label.Text = comboBox.Text
I am trying to get required data into controls when i select specific Student Name from a combobox. i am using SelectedValueChanged event for this and the valuemember for that combobox in order to populate it with required data.
StudentsNamesComboBox.DisplayMember = "StudentName";
StudentsNamesComboBox.ValueMember = "SectionId";
here the studentnameCombobox has a different value from the parameter value required to get specific record from database using storedprocedure.
cmd.Parameters.AddWithValue("#AdmissionNumber", Convert.ToInt16(StudentsNamesComboBox.SelectedValue));
the problem i am facing is that i can't get the required parameter value (#AdmissionNumber) from the selected name in the studentNamesCombox.
any idea how to get the required parameter value without changing the studentNamesCombox valuemember? any other way around? because changing vlauemember will affect the whole functionality.
Regards
When you are populating StudentName combobox, why don't you set the ValueMember of that combobox to "AdmissionNumber".
You do not need SectionI of the student in that combobox. SectionId is already available in the other combobox. In StudentName combobox all the items have same sectionId. Correct me if my assumption is wrong.
This way ComboBox.SelectedValue will give the value associated with the selected item which in your case will be "AdmissionNumber".
To get the name of the selected student you need to use ComboBox.SelectedItem.Text. That will give the Selected StudentName in your case.
I have a Winforms application and a combobox has it's datasource set to a List when the form loads. The data displays fine in the combobox.
Then after a user clicks a button I want to create a new List and assign that List as the datasource for the combobox.
The problem is that after setting the datasource to be the new List the items in the combobox don't change. Here is the code I'm using.
var newPersonList=new List<Person>(){//...};//Person has a property named 'Name'
dlCustomer.DataSource = newPersonsList
dlCustomer.DisplayMember = "Name"
dlCustomer.Refresh()
Does anyone know how to make the correct data be displayed in the combobox the second time I assign new data source for it?
You can clear the items and then set the DataSource as below
dlCustomer.Items.Clear();
dlCustomer.DataSource = newPersonsList;
dlCustomer.DisplayMember = "Name";
In my database I have a table Cars which contains (among other things) an idCategory column which is a foreign key. That foreign key refers idCategory in the Category table which contains the description of the category.
Now in my C# application I got a form that is used to add new cars. There is a combobox to choose the category.
comboboxCat.DataSource = datatable1;
comboboxCat.DisplayMember = "categoryDescription";
comboboxCat.ValueMember = "idCategory";
So I set the DataSource of the ComboBox to the DataTable that contains the data of Category table, and I set the id as the valueMember and the Description as DisplayMember.
I also have a DataSet containing the table Cars.
What I want to do is to automatically update the Car data in the DataSet when the value of the Combobox is changed. I don't know how to bind it. In a datagridview it's easy because I just have to set the DataPropertyName of the DataGridViewComboBoxColumn. But there's not such properties in a normal ComboBox. Thanks in advance
I belive you have to bind the IdCategory field from the Cars DataTable to the SelectedValue of the ComboBox.
Finally found what I was looking for : http://blogs.msdn.com/b/bethmassi/archive/2007/04/25/tips-on-related-data-binding-and-comboboxes.aspx
My answer is to use DataBindings to make the link
I have a comboBox that displays different Municipalities (these Municipalities belongs to a particular Province) in our country. Since there are Municipalities having the same name, I binded the "MunicipalityName" (a table column from 'MUNICIPALITY' table in my database) to DisplayMember property of the comboBox and "Municipality_ID" to ValueMember property of the comboBox.
When the user saves his details, I supply the SelectedValue from ValueMember of the MUNICIPALITY and insert it to Employee table.
cmd.Parameters.Add(new SqlParameter("#Municipality_ID", (object)comboBoxMunicipality.SelectedValue.ToString()));
I find it hard when it comes to retrieval of data when an Employee needs to update his information. I have to manually check the Municipality_ID of that employee and compare it to the binded data in the comboBox, then loop through it, determine what index that Municipality_ID located, and set the SelectedIndex property of the comboBox. (Quiet lengthy compared to code snippet below)
I have this code, but I find conflicts since Municipality_Name is not unique.
//set SelectedIndex based from DisplayMember of the comboBox
comboBoxMunicipality.SelectedIndex = comboBoxMunicipality.FindStringExact(dataTable.Rows[0]["MunicipalityName"].ToString());
Is there a way to set the SelectedIndex of the comboBox like the code above, but this time, comparing it to the ValueMember?
Is there a shortcut?
//something like this?
comboBoxMunicipality.SelectedIndex =
comboBoxMunicipality.FindByValue(dataTable.Rows[0]["Municipality_ID"].ToString());
I hope you get my point guys... Please help. Thanks.
How about this?
comboBoxMunicipality.SelectedValue = theMunicipalityIDtoSelect