Combo box with default value like --Select Gender -- - c#

I have a combo box which I want the default value to be -- Select Gender --
I have tried the following coding, and I got this error
Items collection must be empty before using ItemsSource.
Please help
cboGender.ItemsSource = null;
cboGender.DisplayMemberPath = "Display";
cboGender.SelectedValuePath = "Value";
cboGender.SetBinding(ComboBox.ItemsSourceProperty, oBinding);
cboGender.Items.Insert(0, "--Select Gender--");
cboGender.SelectedIndex = 0;

Add "--Select Gender--" to your Collection before binding it to the ItemsSource (if you want it as first, remember to use Insert(0, ) and then after binding it, set the SelectedIndex to it.

May be you should try to call
cboGender.Items.Clear();
before the insertion of the default item.
OR
Just create your data source with the default value item before inserting any value in the combo box and write something like this:
cboGender.Items.Clear();
cboGender.ItemsSource = dataSource;
where dataSource would be the list with the all items.

You can use
<ComboBox x:Name="cboGender"
Text="--Select Gender--" />
if you don't need to choose this default value again after first selection

Related

add "please select" row in combobox [duplicate]

I have a combobox which is binded to a list. I've been trying to work out how to have the first item in the combobox when it's loaded a "--Please Select--" before the data from the list is loaded.
I have tried this,
cbUpdate.DataSource = _names;
cbUpdate.Items.Insert(0, "-Select-");
cbUpdate.SelectedIndex = 0;
cbUpdate.DisplayMember = "Name";
But this gives the errro,
Items collection cannot be modified when the DataSource property is
set.
I understand the error, but I am unsure of how to solve it. I've trying to set the Text of the combobox to "Please Select" but that doesn't work.
var names = new BindingList<Names>();
You are setting the DataSource and Adding an Item After which will eventually throw an error. A better approach would be to add the item "-Select-" first to the combobox then try to add next the items in the list using a foreach statement instead of binding the list to the combobox.
cbUpdate.Items.Clear();
cbUpdate.Items.Add("-Select-");
foreach (string item in thelsit)
{
cbUpdate.Items.Add(item.ToString());
}
cbUpdate.SelectedIndex = 0;
You have to decide, either use the Items property, and fill in all your options. Or use a DataSource with all the options.
In any case, for the "--Please Select--" entry, you either need to have it as one of the options inside the Items, or DataSource. Or you could just set the SelectedText property.
Try inserting the '--Select--' text in the names list before setting it to the datasource.
Example:
List<Person> list = new List<Person>() {
new Person("Jon"),
new Person("Ram"),
new Person("Rin")
};
list.Insert(0, new Person("--Select"));
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Name";
comboBox1.Items.Clear();
There are other ways to achieve this. It depends on how you are retrieving the values from your database.
If you are using a select statement, you can do a union like
// Oracle
SELECT 0, "---- Please Select ----" from dual
UNION
SELECT [value], [name] from [table]
The result of which you can assign as the data source.
Another option is to remove the DataSource property and once you have the data in hand, iterate through the record set and add items one by one, with 0 and "---- Please Select ----" as the ValueMember and DisplayMember for the first item.

DataSource, bind more then 1 value

I want to bind more then 1 columns to drop down list, so that I can get the column values when user clicks a button,
ddlListMine.DataSource = GetSomeChickens();
ddListMine.DataTextField = "ChickenName";
ddListMine.DataValueField= "NumberOfEggsChickenLay";
ddListMine.Items.Insert(0, new ListItem("Please Please Please Select....", "0"));
ddListMine.DataBind();
I have another column "ChickenType", which I want to access in Selected Index change column.
GetSomeChickens(); returns 6 columns, including ChickenName, NumberOfEggsChickenLay, ChickenType and so on...
Edit
Off course, I can call database again in selected index change method, but there must be a way around i think
The DropDownList doesn't hold the entire object during the binding, only the Text and Value as defined by DataTextField and DataValueField.
In order to get a selected object back, you can have a method to get the ChickenType by passing the ChickenName using Linq like this.
List<Chicken> Chickens = GetSomeChickens();
Var Chicken= Chickens.FirstOrDefault(c => c.ChickenName== ddlListMine.SelectedItem.Text);
if(Chicken!= null)
{
string ChickenType = Chicken.ChickenType ;
}

Add Items to the ComboBox

I have a ComboBox control.
I bind to this control to the DataSet table.
Here is the code:
comboBox.Items.Add(("Select"));
comboBox.DataSource = DataSet.ColorTable;
comboBox.DisplayMember = DataSet.ColorTable.ColorNameColumn.ColumnName;
comboBox.ValueMember = DataSet.ColorTable.ColorIDColumn.ColumnName;
This result I get:
I want to display on the top of the list SELECT: word. So I need to add addition Item to the comboBox control.
Here how I implement it:
cmbCategory.Items.Add(("Select"));
But the result is still the same as above. I get only colors without SELECT: word on the top of the list.
Any idea how I can add this string-SELECT: to the ComboBox control and set to this string ValueMember?
Use Insert method instead.
cmbCategory.Items.Insert(0, "Select");
Note : Put this code after the databind.
You can add the collections of color to an array or a dataset (if you are getting them from database) first and then add items "select", then add each elements of the array or a column of the dataset.
Do this in Form_Load function and wherever there are changes made in color collections array or database.
//This will set Display member and value member
comboBox.DisplayMember = "ColorName";
comboBox.ValueMember = "ColorCode";
//This will add a new row to table in binded dataset
DataRow dr = dal.MyProperty_dsColors.Tables["ColorInfo"].NewRow();
dr["ColorName"] = "Select Color"; //SomeName
dr["ColorCode"] = 001; //Some ID
dal.MyProperty_dsColors.Tables["ColorInfo].Rows.Add(dr);
//binding dataSource
comboBox.DataSource = dal.MyProperty_dsColors.Tables["ColorInfo"];
What would also help you is that you set the ComboBox without having to 'Select' it when the popup arrives...
Select your ComboBox, under the properties tab, select Appearance->Drop Down Style and select DropDownList.
If we want to add manually values in a combobox (for example integers) this can be done using a for loop:
// sample code
int lower=1;
int higher=500;
for (int i=lower; i<=higher; i++)
combo_values.Items.Add(i.ToString());
Note that you have to use the int.Parse(combo_values.Text) command to read a value.

Combobox textChanged event not firing

We have customar table contains Cust_ID, Cust_Name etc.....
for this table Cust_Name is not unique and one Customer name can repeat Number of times.
i am getting data from SQL and binding to ComboBox (winform)
cmbCustomar.Datasource = GetCustomerData(_LocationID);
cmbCustomar.DisplayMember = "Cust_Name";
cmbCustomar.ValueMember = "Cust_ID";
Here the Problem is :
Customer Name : JOHN is repeated 4 times, all Cust_ID are different
when user select JOHN on first Item i am getting correct "SelectedValue"
but if user select 2 nd or 3rd JOHN
Combobox Item it allways default select First Item (Name as JOHN)
and the SelectedValue allways return the First Item Value.
i am not able to find where i am doing wrong, please suggest.
Try changing the following property:
cmbCustomar.DropDownStyle = DropDownList;
If your ComboBox has DropDownStyle = DropDown, then the "text" part of the ComboBox is trying to match the first item it can find in the list, and in this case, it ignores the current selected item and finds the first "John" on your list.
Keep in mind "SelectedValueChanged" event will fire when combobox being populated. Make sure to un-subscribe to this event before populating the combobox. and subscribe again after populating the data.
//unsubsribe the event before populating combobox1
this.cmbCustomar.SelectedValueChanged -= new System.EventHandler(this.cmbCustomar_SelectedValueChanged);
cmbCustomar.Datasource = GetCustomerData(_LocationID);
cmbCustomar.DisplayMember = "Cust_Name";
cmbCustomar.ValueMember = "Cust_ID";
//subscribe the event again
this.cmbCustomar.SelectedValueChanged += new System.EventHandler(this.cmbCustomar_SelectedValueChanged);
It seems that you are very new to it. And as by my guess, because of your incomplete description, you are trying return the "Selected Value" based on the selected text of the combobox. But rather than that you must try to attach a value a to selected text and return that value. It will surely solve your problem.
Hope it helps.

Remove Default Selection from a DataBound ComboBox

I have data bind a combo box with a list of values in the database, now, the first record is being displayed as a default value, i need to change this and set to blank or my custom message, any solution?
You can also set the Text property of the ComboBox directly to get a custom message, such as:
comboBox1.SelectedIndex = -1;
comboBox1.Text = "Select an item";
Adding an empty item as the first item to the DataSource before binding is the ugly fix for this issue.
Setting the SelectedIndex as -1 might help.
Below is another option, but you may have to validate when retrieving the selected item.
comboBox.Text = string.Empty;
After binding data to your combobox, insert new item at index 0:
combobox1.Items.Insert(0, "Default Value");
or
combobox1.Items.Insert(0, ""); //Empty

Categories