单击按钮后刷新组合框 [英] refresh a comboBox after button click

查看:96
本文介绍了单击按钮后刷新组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有comboBox,textBox和按钮的表单。 comboBox使用dataBindingSources显示sql数据库中的项目。在填充textBox并单击按钮后,一个新项目被添加到此数据库,但我无法在comboBox中看到这个新项目,直到我关闭并重新打开windowsForm。如何刷新comboBox而不关闭/重新打开???



这里是代码:



i have a form with a comboBox, a textBox, and a button. the comboBox shows items from an sql database using dataBindingSources. after filling the textBox and clicking the button, a new item is added to this database, but i can''t see this new item in the comboBox until i close and re-open the windowsForm. how to refresh the comboBox without close/re-open???

here''s the code:

private void button2_Click(object sender, EventArgs e)
        {
            {

                SqlConnection myConnection = new SqlConnection("****");
                myConnection.Open();
                
                try
                {
                    SqlCommand myCommand = myConnection.CreateCommand();
                    myCommand.CommandText = "INSERT INTO Items (NameItem) " +
                "Values ('" + textBox2.Text + "')";

                    myCommand.ExecuteNonQuery();
                    MessageBox.Show("Item Added", "OK");
       
                }
                catch (SqlException exp)
                {
                    MessageBox.Show(exp.Message);
                }

                finally
                {
                    
                    myConnection.Close();
                    textBox2.Clear();
                    
                    
                }
              
            }

推荐答案

重新绑定组合框在添加项目后,在按钮单击事件中。
Rebind the combo box in your button click event, after adding the item.


这篇关于单击按钮后刷新组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆