Skip to content

Remove item from combo box once it's been selected

Ronok Bhowmik

Ronok Bhowmik

Asked Jan 18, 2014, 11:43 AM EST3 answers
Share
LinkedInX
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 Mohapatra
    0


    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

    0
  • Ronok Bhowmik
    0
    This is my code.Help to find the exact location to put your code
    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());
                }
            }
    0
  • Biswa Pujarini Mohapatra
    0


    here is your code

    Java
    //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());
                }
            }
    0