Deleting databound combobox item in database

Hi, I have been searching for answers everywhere with no luck. I know that comboboxes are kind of buggy and alot of people seem to have problems with them.

My problem is trying to remove a row from the database while it is databound to a combobox. Here is my situation: a form loads a databound list of unique usernames. The user can select usernames and choose to edit, create new, or delete usernames.

I have gotten edit and create new to work however when I try to delete I get errors saying that the items cannot be edited while being databound.

Here is some example code that I have tried:

Dim hold As Integer
hold = ComboBox1.SelectedIndex
fp.Position = hold
fp.RemoveAt(hold)

DaEditUsers.Update(DsEditUsers1)
fp.Refresh()


Answer this question

Deleting databound combobox item in database

  • emgreenberg

    Hi bclark,

    You cannot modify the items in your comboBox when it is binded to a datasource.
    Do the editing on the DataTable of your DataSet. Then call the function which populates your comboBox. I hav here a sourcode that inserts a blank item on the comboBox since it is binded to a datasource.

    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();

    Hth,

    Michael Castillones, C# MCAD



  • tgroebner

    Your welcome clark.

  • ZAiNT

    Thanks for the help Michael, I'll give it a try
  • Deleting databound combobox item in database