none
Add an empty item to bound combobox

    Question

  • I have a combobox and I bind it to a DataTable. I want it to have an empty item but I can't Add it. Isn't there a way to do this with reflection or with some other technique?

    I can't add an empty row to the data table, because I have int columns and the empty item I want to add is a string.
    Saturday, March 22, 2008 2:12 AM

Answers

  • Hi Boyan Mihailov,

     

    Based on my understanding, you want to add an empty item into your ComboBox control, however, the ComboBox is binding to a DataTable. If I have misunderstood you, please feel free to tell me, thanks.

     

     

    When the ComboBox control binds to DataSource, you cannot update Items of it. Refer to the following code from Reflector:

     

    Code Snippet
    public int Add(object item)
    {
        this.owner.CheckNoDataSource();
        int num = this.AddInternal(item);
        {
            this.owner.SetAutoComplete(false, false);
        }
        return num;

    }   

     

     

    private void CheckNoDataSource()

            {

                if (this.DataSource != null)

                {

                    throw new ArgumentException(SR.GetString("DataSourceLocksItems"));

                }

            }

     

     

    So If you want to add empty items to ComboBox, so can insert empty DataRow in DataTable work

    around.

    hope the following code can help you.

    Code Snippet

           

    private void TestSimpleBindingForm_Load(object sender, EventArgs e)

            {

                PopulateComboBox();

            }

     

            private DataTable PrepareDataSource()

            {

                DataTable dt = new DataTable();

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

                dt.Columns.Add("Name", typeof(string));

     

                   dt.Rows.Add(DBNull.Value, DBNull.Value);

                dt.Rows.Add(1, "Testname One");

                dt.Rows.Add(2, "Tetstname Two");

     

                return dt;

            }

            private void CheckNoDataSource()

            {

                if (this.DataSource != null)

                {

                    throw new ArgumentException(SR.GetString("DataSourceLocksItems"));

                }

            }

            private void PopulateComboBox()

            {

                this.comboBox1.DisplayMember = "Name";

                this.comboBox1.ValueMember = "ID";

                this.comboBox1.DataSource = PrepareDataSource();

            }

     

     

     

     

     

    Regards,

    Roy Zhou

     

    Wednesday, March 26, 2008 7:41 AM

All replies

  • Hi,

    Whenever you want an empty item in a comboBox, you can add one after databinding.
    So the steps to follow are:

    • create / retrieve the datasource
    • bind the datasource to the combobox
    • execute ComboBox.Items.Insert(0, string.Empty)   (or a string like 'Please select')

    Hope this helps.

    Saturday, March 22, 2008 9:36 AM
  • No, this does not help. An exception is thrown - Items collection cannot be modified when the DataSource property is set.
    Saturday, March 22, 2008 10:31 AM
  • > Items collection cannot be modified when the DataSource property is set.

    Thats true for a WinForms ComboBox, the technique Rick described was for ASP.NET.

     

    You need to add a row before binding.  You're right that int can not be empty - so what value do you want to see when the empty row is selected?  Adding a row with DBNull.Value in your integer column would work - this would show up as blank in the combobox.

     

    Or you could bind to a DataTable with at least two columns: one column with your integer value and a second string column with the value you want to display in your combobox (e.g. int.ToString for your integer values, and "Please Select" for the DBNull value).  When you bind you'd set DisplayMember to the string  column and ValueMember to the int column.

     

    Without knowing more it's difficult to recommend the best approach.

     

     

    Saturday, March 22, 2008 5:24 PM
  • i prefer you would be more clear but here an example:

     

    - if you are getting your data from an an sql query you can add the value to the query

    or

    - add the row to the datatable before binding the control on it ex:

     

     

    DataRow dr = YourDataTable.NewRow();//add the new row

    dr["Column1"] = Value1;

    dr["Column2"] = value2;

     

    YourDataTable.Rows.Add(dr);

     

    then you should bind the combo box on that datatable

     

    cmb.DataSource = dt;

    cmb.DisplayMember = "Column2";

    cmb.ValueMember = "Column1";

     

    hope this was helpful Smile

    Sunday, March 23, 2008 12:27 PM
  • I want to create a custom user control. It has a datagridview control. Also for each column in this datagridview there is a combobox and a button. Information from each column is added to the corresponding combobox. The button is for ordering the data by corresponding column. I have a BIndingSource for filtering and ordering the data in the Grid. And I want to have an item <View All> in the begining of each combobox and when this item is selected the filter for the corresponding column is removed. The problem is that some of the columns are integers and I cannot add a new row in the data table.
    Sunday, March 23, 2008 3:33 PM
  • > And I want to have an item <View All> in the begining of each combobox

     

    In this case, if you are using databinding, you need to bind to a data source whose DisplayMember property/column is a string.  The ValueMember can still be an integer.   E.g. try adding a row to your DataTable with DBNull.Value in the integer column.

    Monday, March 24, 2008 10:16 AM
  • Hi Boyan Mihailov,

     

    Based on my understanding, you want to add an empty item into your ComboBox control, however, the ComboBox is binding to a DataTable. If I have misunderstood you, please feel free to tell me, thanks.

     

     

    When the ComboBox control binds to DataSource, you cannot update Items of it. Refer to the following code from Reflector:

     

    Code Snippet
    public int Add(object item)
    {
        this.owner.CheckNoDataSource();
        int num = this.AddInternal(item);
        {
            this.owner.SetAutoComplete(false, false);
        }
        return num;

    }   

     

     

    private void CheckNoDataSource()

            {

                if (this.DataSource != null)

                {

                    throw new ArgumentException(SR.GetString("DataSourceLocksItems"));

                }

            }

     

     

    So If you want to add empty items to ComboBox, so can insert empty DataRow in DataTable work

    around.

    hope the following code can help you.

    Code Snippet

           

    private void TestSimpleBindingForm_Load(object sender, EventArgs e)

            {

                PopulateComboBox();

            }

     

            private DataTable PrepareDataSource()

            {

                DataTable dt = new DataTable();

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

                dt.Columns.Add("Name", typeof(string));

     

                   dt.Rows.Add(DBNull.Value, DBNull.Value);

                dt.Rows.Add(1, "Testname One");

                dt.Rows.Add(2, "Tetstname Two");

     

                return dt;

            }

            private void CheckNoDataSource()

            {

                if (this.DataSource != null)

                {

                    throw new ArgumentException(SR.GetString("DataSourceLocksItems"));

                }

            }

            private void PopulateComboBox()

            {

                this.comboBox1.DisplayMember = "Name";

                this.comboBox1.ValueMember = "ID";

                this.comboBox1.DataSource = PrepareDataSource();

            }

     

     

     

     

     

    Regards,

    Roy Zhou

     

    Wednesday, March 26, 2008 7:41 AM
  • You can do this very simple by setting SelectedIndex to -1.

    ComboBox c = new ComboBox();
    c.DataSource = myDatasource;
    c.SelectedIndex = -1;
    Monday, November 23, 2009 9:53 AM
  • The problem with setting SelectedIndex to -1 is that fixes the problem only when the form is first displayed. If the user pulls down on the combo box and makes a selection, then decides an empty value is the appropriate choice, there is no going back. Pulling down on the combo box a second time will not present an empty value to choose from.
    Thursday, December 29, 2011 4:56 AM