none
how to insert blank in combobox after databound

    Question

  •  

    What event in a windows form application would be appropriate to insert a blank like in index 0 after databound?  I don't see any databinding or databound events.

    combobox.items.insert(0, "");
    Monday, December 24, 2007 9:37 PM

Answers

  • If your ComboBox is data bound, you cannot insert an item by using the ComboBox.Items.Insert() method. You have to insert the empty item in the data source. And you don't need an event to insert the item, just insert the empty item to the data source right after you assigning DataSource value for the ComboBox, something like this

    Code Block


                DataTable dt = new DataTable();

                dt.Columns.Add("id", typeof(int));

                dt.Columns.Add("name");

                for (int j = 0; j < 10; j++)

                {

                    dt.Rows.Add(j, "row" + j.ToString());

                }

                this.comboBox1.DisplayMember = "name";

                this.comboBox1.ValueMember = "id";

                this.comboBox1.DataSource = dt;

     

                dt.Rows.InsertAt(dt.NewRow(), 0);

                this.comboBox1.SelectedIndex = 0;

     

     

    Tuesday, December 25, 2007 1:42 PM

All replies

  • If your ComboBox is data bound, you cannot insert an item by using the ComboBox.Items.Insert() method. You have to insert the empty item in the data source. And you don't need an event to insert the item, just insert the empty item to the data source right after you assigning DataSource value for the ComboBox, something like this

    Code Block


                DataTable dt = new DataTable();

                dt.Columns.Add("id", typeof(int));

                dt.Columns.Add("name");

                for (int j = 0; j < 10; j++)

                {

                    dt.Rows.Add(j, "row" + j.ToString());

                }

                this.comboBox1.DisplayMember = "name";

                this.comboBox1.ValueMember = "id";

                this.comboBox1.DataSource = dt;

     

                dt.Rows.InsertAt(dt.NewRow(), 0);

                this.comboBox1.SelectedIndex = 0;

     

     

    Tuesday, December 25, 2007 1:42 PM
  • Thank you for the info Zhi-Xin Ye - MSFT

     

    I modified this concept to use a generic list instead of a datatable. 

     

    List<PersonStruct> NameList = new List<PersonStruct>();

     

    cbName.Items.Clear();

    PersonStruct xSelect = new PersonStruct();

    xSelect.ID = 0;

    xSelect.Name = "~ Select ~";

    NameList = RetrieveNames();  //Returns a generic based on a structure populate from a dataset

     

    // Compact Edition limitation - cannot insert to the Combobox, you must insert into the datasource

    NameList.Insert(0, xSelect);

     

    // Guard clause for data set passed in

    if (NameList != null)

    {

    cbName.DisplayMember = "Name";

    cbName.ValueMember = "ID";

    cbName.DataSource = NameList;

    }

     

     

    Wednesday, January 09, 2008 9:51 PM
  • As I was using a dataset and not a data table,

    I modified the code as

                    ds.Tables[0].Rows.InsertAt(ds.Tables[0].NewRow(), 0);
                    cmb.DataSource = ds.Tables[0];
                    cmb.ValueMember = "Code";
                    cmb.DisplayMember = "Name";

    Tuesday, February 26, 2008 12:22 PM
  • datatable dt = null;

    dt.Rows.InsertAt(dt.NewRow(), 0);

    combobox.datasource = dt;

     

    Tuesday, January 03, 2012 12:48 AM