Remove item from combo box once it's been selected
I have a textbox ,a button named insert and a combobox .The comboBox loads data from the database.What I would like to do is to remove data from the combobox if the selected data has been inserted once. Let's i have data loaded in the combobox from the database are Alex, Ronok, Jonny and so on.In the textbox i put 1 and select Alex for that from combobox loaded data.Now insert it into the database using insert button.When the next time the combobox loaded the combobox item Alex disappears.How can i implement it using stored procedure.Please help me to solve it.Thanks in advance.
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Biswa Pujarini MohapatraRank #463 · 5.4K pointsPosted Jan 18, 2014, 5:22 PM EST
After successfully insert to the database return the value and onload event write the code
comboBox1.Items.Remove(comboBox1.selectedItem)
Note:will only remove from the combobox, not from the datasource binded to the combobox. You may remove it from the datasource and re bind the source if you are using datasource to bind comboBox
Ronok Bhowmik28 pointsPosted Jan 18, 2014, 8:50 PM EST
private void BindNamemarks()
{
try
{
SqlConnection conn = new SqlConnection(dataconnection);
SqlCommand cmd = new SqlCommand("uspmarks", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "marks");
//NameComboBox.Items.Remove(NameComboBox.selectedItem);
NameComboBox.SelectedValuePath = ds.Tables[0].Columns["Id"].ToString();
NameComboBox.DisplayMemberPath = ds.Tables[0].Columns["Name"].ToString();
NameComboBox.ItemsSource = ds.Tables[0].DefaultView;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void InsertButton_Click(object sender, RoutedEventArgs e)
{
try
{
{
SqlConnection conn = new SqlConnection(dataconnection);
conn.Open();
SqlCommand cmd = new SqlCommand("USPInsertion", conn);
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", NameComboBox.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Saved Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
Biswa Pujarini MohapatraRank #463 · 5.4K pointsPosted Jan 18, 2014, 11:52 PM EST
here is your code
//added optional parameter to pass combobox value after successfully record inserted private void BindNamemarks(string cmbName = "") { try { SqlConnection conn = new SqlConnection(dataconnection); SqlCommand cmd = new SqlCommand("uspmarks", conn); cmd.CommandType = CommandType.StoredProcedure; conn.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds, "marks"); NameComboBox.SelectedValuePath = ds.Tables[0].Columns["Id"].ToString(); NameComboBox.DisplayMemberPath = ds.Tables[0].Columns["Name"].ToString(); NameComboBox.ItemsSource = ds.Tables[0].DefaultView; //if record successfully inserted then remove from the if (!string.IsNullOrEmpty(cmbName)) NameComboBox.Items.Remove(cmbName); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void InsertButton_Click(object sender, RoutedEventArgs e) { try { { SqlConnection conn = new SqlConnection(dataconnection); conn.Open(); SqlCommand cmd = new SqlCommand("USPInsertion", conn); cmd.Connection = conn; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", NameComboBox.Text); cmd.ExecuteNonQuery(); MessageBox.Show("Data Saved Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information); //call you method to bind combox again BindNamemarks(NameComboBox.Text); conn.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }