Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a combobox bound to a WCF service that populates a list of reports.
I don't want to display an item immidiately, I want the selected index to say
"Please select a report from list".
So I created an item and added it to the ComboBox.
Items List after I bound the control as
comboBox1.ItemsSource = myData;
                    comboBox1.DisplayMemberPath = "Product_Name";
                    comboBox1.SelectedValuePath = "Product_Name";
                    comboBox1.SelectedValue = "ProdID";

                    ComboBoxItem item = new ComboBoxItem();
                   
                    item.Content = "Select";
                    item.Tag = "1";

                    comboBox1.Items.Add(item);
                    //comboBox1.Items.Insert(0, item);


But it gives error message that "operation not supported on read-only collection"
Is there a way to display default text on a combobox?

Thanks in advance.
Posted

Try this, i have tried it and working well

List<Products> result1 = new List<Products>();

                    Products obj = new Products();

                    obj.ProdID = 0;

                    obj.Product_Name= "Select";
                    result1.Add(obj);
                    result1.AddRange(myData);

                    comboBox1.ItemsSource = result1;

                    comboBox1.DisplayMemberPath = "Product_Name";
                    comboBox1.SelectedValuePath = "Product_Name";
                    comboBox1.SelectedValue = "ProdID";

                    comboBox1.SelectedIndex = 0;
 
Share this answer
 
Dear Brother,
Simplest way of doing so is following:

Write the following code on the form containing your combobox.

private void frmMainForm_Load(object sender, EventArgs e)
{
cmbDepartment.SelectedIndex = 0;
cmbUniversity.SelectedIndex = 0;
}
Also make sure u have written "[Select Any]" on the first menu in the combobox.
I 've devised this myself and found it perfect.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900