Is there a way to add a blank Item on data binded Combox?

Hi all,

Is there a way to add a blank Item on data binded Combox without adding new balnk row on the Table or new blank item on Arraylist

Thanks.



Answer this question

Is there a way to add a blank Item on data binded Combox?

  • pallavi19321

    Hi,

    The code posted above this post is for web application

    For windows forms try this: comboBox1.Items.Add("");

    hth,

    Michael Castillones



  • mlamb

    What i did is, when i add a new row i specify the value for my keyfiled as zero.

    something like this.

    dsDataSet.Clear();
    theAdapter.Fill(dsDataSet, "Table");
    tblTable = dsDataSet.Tables("Table");
    myRow = tblTable.NewRow;

    myRow("DisplayField") = "";

    //------------------------
    myRow("KeyField") = 0; // ------> keyfield as ZERO
    //--------------------------

    tblTable.Rows.InsertAt(myRow, 0);
    objCombo.DataSource = null;
    objCombo.Items.Clear();
    objCombo.DataSource = tblTable;
    objCombo.DisplayMember = "DisplayField";
    objCombo.ValueMember = "KeyField";


  • Inuya5ha

    what I mean is the combox is binded to a Datatable or Arraylist something like this

    ComboBox1.DataSource = DataTable or ArrayList

    ComboBox1.ValueMember = "ID"

    ComboBox1.DisplayMember ="ValueName"

    IF I used the Item.ADD("") theres gonna be an error. I have a solution with this but i dont like the concept. what I did is before I set the combox datasource I add a Blank row on Datatable or Arraylist. Is there any other way to do this by not adding a blank row .


  • Jesse Cheng

    You can put this in the Page_Load:

    yourComboBox.Items.Insert(0, New ListItem(" "))

    This will create a blank space in your Combobox.


  • Jay G

    Hi,
    I did the same thing and it worked absolutely fine but there is one problem.
    After I bind the combobox the way you discribed,
    If i do following
    cbo.selectedvalue= <value of the first item after blank item>
    it is throwing an exception.
    ------------
    In Method: Void set_SelectedIndex(Int32)

    Specified argument was out of the range of valid values.
    Parameter name: '-2147483648' is not a valid value for 'index'.
    -----------

    Any idea why is this happening

    here using selected value i am selecting the first item after the blank item.


    Thanks
    Lav

  • Johan Cyprich

    Hi Teo,

    You cannot add a blank item on combobox once's it is binded to a datasource. Iterate your code using Datatable datasource. First you do your adding of blank item to datatable then bind it as datasource.

    Here is a code example:

    DataSet dsDataSet = new DataSet();
    DataTable tblTable;
    DataRow myRow;
    SqlConnection myConn;
    myConn = new SqlConnection();
    myConn.ConnectionString = gsConnStr;
    myConn.Open();
    theAdapter = new SqlDataAdapter(pSQL, myConn);
    dsDataSet.Clear();
    theAdapter.Fill(dsDataSet, "Table");
    tblTable = dsDataSet.Tables("Table");
    myRow = tblTable.NewRow;
    myRow("DisplayField") = "";
    tblTable.Rows.InsertAt(myRow, 0);
    objCombo.DataSource = null;
    objCombo.Items.Clear();
    objCombo.DataSource = tblTable;
    objCombo.DisplayMember = "DisplayField";
    objCombo.ValueMember = "KeyField";
    objCombo.SelectedValue = 0;
    dsDataSet = null;
    tblTable = null;
    myConn.Close();

    Hope It Helps,


    Michael Castillones, MCP



  • Is there a way to add a blank Item on data binded Combox?