25

In windows form, It has a ComboBox, Which have data binded by the DataSource.

When going to set the text property for a ComboBox.

Selected ComboBox -> Property -> Text : "--Select--".

Design page shows the given text. But when run the application the given text disappeard and the initial index value of a comboBox item appeared, Which is from the DataSource.

So i gave the ComboBox text in the Form load. I mean in the Constructor

public myform()
{
     InitializeComponent();
     ComboBox.Text="--Select--";
}

link revised and more. But ..

Setting default item in combo box

https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text(v=vs.110).aspx

Searched lot of question in SO depends to ComboBox. But those never solve my case

Edited

enter image description here

In that combobox, Click the right top corner , From that i choosed data for my combobox by using Datasouce. I didn't write any code for add items into combobox.

User6667769
  • 745
  • 1
  • 7
  • 25
  • 3
    Are you inserting that item in combobox by `ComboBox.Items.Insert(0, "--Select--");` and setting selectedindex by `ComboBox.SelectedIndex = 0;` – Vandita May 05 '17 at 10:21
  • Thanks for your reply @Vandita I bounded data by using Datasource. So can't add a new value. – User6667769 May 05 '17 at 10:22
  • You can add that after code of dat binding – Vandita May 05 '17 at 10:24
  • 2
    If you set the DataSource property then you cannot change Text property from code. Add the "--Select--" text as a fake item inside the datasource (this depends on what kind of datasource you have) – Steve May 05 '17 at 10:24
  • 1
    I suspect that the binding is executed after your code, which makes the text disappear. Can't you add a new item to the list that is bound? – user1845593 May 05 '17 at 10:25
  • http://stackoverflow.com/questions/11374880/insert-item-in-combobox-after-binding-it-from-a-dataset-in-c-sharp – Mahdi May 05 '17 at 10:30
  • @user1845593 thanks, Can't understantd, you mean binding is executed after the Constructor ah? – User6667769 May 05 '17 at 10:31
  • You did bind a DataSource to combobox, right? the Binding happens after the Constructor. If you subscribe the event BindingContextChanged of combobox you will see that is fired after the constructor. So that makes your text to disappear. If I was you, I added that value to the datasource – user1845593 May 05 '17 at 10:34
  • @Mahdi It makes ArgumentException , Can't bind to the new value member. – User6667769 May 05 '17 at 11:05

2 Answers2

35

You can do something like this:

    public myform()
    {
         InitializeComponent(); // this will be called in ComboBox ComboBox = new System.Windows.Forms.ComboBox();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'myDataSet.someTable' table. You can move, or remove it, as needed.
        this.myTableAdapter.Fill(this.myDataSet.someTable);
        comboBox1.SelectedItem = null;
        comboBox1.SelectedText = "--select--";           
    }
Vandita
  • 708
  • 4
  • 13
  • Why ? You can keep a check that if selectedindex != 0 and then continue your code on SelectedIndexChange event – Vandita May 05 '17 at 10:44
  • 2
    @Vandita this code cannot work. You can't change the Items collection once you set the datasource. You get an ArgumentException. – Steve May 05 '17 at 10:46
  • @User6667769: You must be initializing a datasource which would be getting added in `private void Form1_Load(object sender, EventArgs e)` Add `BindComboBox()` in that Load method..I have tested this and it works ! – Vandita May 05 '17 at 11:14
  • Hey In that combobox, Click the right top corner , From that i choosed data for my combobox by using Datasouce. I didn't write any code for add items into combobox.. So its automatically set's the 1st index value for the data to be index 0. so how can i add the new data into the 0th index. Its makes exception – User6667769 May 05 '17 at 11:19
8

Suppose you bound your combobox to a List<Person>

List<Person> pp = new List<Person>();
pp.Add(new Person() {id = 1, name="Steve"});
pp.Add(new Person() {id = 2, name="Mark"});
pp.Add(new Person() {id = 3, name="Charles"});

cbo1.DisplayMember = "name";
cbo1.ValueMember = "id";
cbo1.DataSource = pp;

At this point you cannot set the Text property as you like, but instead you need to add an item to your list before setting the datasource

pp.Insert(0, new Person() {id=-1, name="--SELECT--"});
cbo1.DisplayMember = "name";
cbo1.ValueMember = "id";
cbo1.DataSource = pp;
cbo1.SelectedIndex = 0;

Of course this means that you need to add a checking code when you try to use the info from the combobox

if(cbo1.SelectedValue != null && Convert.ToInt32(cbo1.SelectedValue) == -1)
    MessageBox.Show("Please select a person name");
else
    ...... 

The code is the same if you use a DataTable instead of a list. You need to add a fake row at the first position of the Rows collection of the datatable and set the initial index of the combobox to make things clear. The only thing you need to look at are the name of the datatable columns and which columns should contain a non null value before adding the row to the collection

In a table with three columns like ID, FirstName, LastName with ID,FirstName and LastName required you need to

DataRow row = datatable.NewRow();
row["ID"] = -1;
row["FirstName"] = "--Select--";    
row["LastName"] = "FakeAddress";
dataTable.Rows.InsertAt(row, 0);
Steve
  • 213,761
  • 22
  • 232
  • 286