在数据库表中插入新值时刷新我的列表框 [英] Refresh my listbox when new value is inserted in db table

查看:135
本文介绍了在数据库表中插入新值时刷新我的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个ListBox,用作DataGridView的控件/过滤器.同样,我还具有插入功能来创建新公司.

现在,当用户创建新公司时,我想更新列表框以包含创建的新公司.

Hi I have created a ListBox that works as a controler/filter for my DataGridView. In the same form I also have a insert function to create a new company.

Now when a user creates a new company I want to update the listbox to contain the new company created.

namespace MyNameSpace
{
    // Creating our Listbox, Datatable and DataGridView for later use
    public partial class CompanyForm : Form
    {
        private ListBox companyList = new ListBox();
        private DataTable dataEmployeesSource = new DataTable();
        private DataGridView dataEmployees = new DataGridView();

        //Connection String
        string cs = ConfigurationManager.ConnectionStrings["MyCS"].ConnectionString;

        // we initialize our methods but dont load them
        public CompanyForm()
        {
            InitializeComponent();

            initDataEmployees();
            initCompanyList();

            companyList.SelectedIndexChanged += listbox1_SelectedIndexChanged;
        }

        // The first DataEmployees method to fill our DataGridView is executed
        private void initDataEmployees()
        {
            const string sql = "Select fname, ename, c.companyName AS companyName FROM dbo.Users u inner join dbo.Company c on c.companyName = u.company;";
            dataEmployeesSource = selectIntoDataTable(sql);
            dataEmployees.DataSource = dataEmployeesSource;
        }

        // Method to fill our CompanyList which later works as a filter for our DataGridView
        private void initCompanyList()
        {
            const string sql = "Select companyName from dbo.Company";
            try
            {
                DataTable dt = selectIntoDataTable(sql);
                companyList.DataSource = dt;
                companyList.ValueMember = "companyName";
            }
            catch (Exception ex)
            {
                MessageBox.Show("There are no companies to display");
            }
        }
        // Filling our DataTable locally to enable fast filtering
        private DataTable selectIntoDataTable(string sql)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(cs))
            {
                try
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    using (SqlDataAdapter a = new SqlDataAdapter(cmd))
                    {
                        a.Fill(dt);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    con.Close();
                }
            }
                return dt;
            }
        // Here we show the changed value based on the selected index from our CompanyList
        private void listbox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView selectedRow = (DataRowView)companyList.SelectedItem;
            string selectedText = (string)selectedRow.Row["companyName"];
            DataView dv = dataEmployeesSource.DefaultView;
            string filter = string.Format("companyName = '{0}'", selectedText);
            dv.RowFilter = filter;
            dataEmployees.DataSource = dv;
        }
        // Create company, we create a company here and insert it into our dbo.Company table
        private void createCompany_Click_1(object sender, EventArgs e)
        {
            if (textBoxCompanyName.Text == "")
            {
                MessageBox.Show("Vänligen fyll i informationen");
                return;
            }
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into dbo.Company (companyName) values(@companyName)", con);
                cmd.Parameters.AddWithValue("@companyName", textBoxCompanyName.Text);
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                MessageBox.Show("Grattis! Du har skapat ett företag");

            }
        }
    }
}



我尝试过的事情:

我试图在createCompany_Click方法的末尾添加更新.



What I have tried:

I have tried to add updates in the end of the createCompany_Click method

推荐答案

您没有在createCompanyClick处理程序中调用cmd.ExecuteNonQuery().
You''re not calling cmd.ExecuteNonQuery() in your createCompanyClick handler.


这篇关于在数据库表中插入新值时刷新我的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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