简体   繁体   English

对DataGridView的更改未反映在数据库中

[英]Changes to DataGridView aren't being reflected in database

I have a DataGridView control and a save button. 我有一个DataGridView控件和一个保存按钮。 When the Save button is clicked, I want any changes made to the DataGridView to be reflected in my database via a DataAdapter Update() command. 单击“保存”按钮后,我希望通过DataAdapter Update()命令将对DataGridView所做的任何更改反映到数据库中。 However, after hitting the save button and reloading the form, the updates are not there. 但是,点击保存按钮并重新加载表单后,更新就不存​​在了。

Here is all the code for the Save button currently: 这是当前“保存”按钮的所有代码:

private void btnSave_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=addtool.database.windows.net;Initial Catalog=AddToolToInventoryDB;User id=kanerbw; Password=Rabaraba!11;");
        DataTable dt = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter("select * from ToolTB", con);
        SqlCommandBuilder myBuilder = new SqlCommandBuilder(adapter);

        con.Open();
        SqlCommandBuilder scb = new SqlCommandBuilder(adapter);
        myBuilder.GetUpdateCommand();
        adapter.UpdateCommand = myBuilder.GetUpdateCommand();
        adapter.Update(dt);
        con.Close();
    }

EDIT: I had forgotten to set the datasource of my DGV to the datatable. 编辑:我忘记了将DGV的数据源设置为数据表。 Here's the working code: 这是工作代码:

private void btnSave_Click(object sender, EventArgs e)
    {
        using (var con = new SqlConnection(connectionString))
        {
            SqlDataAdapter adapter = new SqlDataAdapter("select * from ToolTB", con);
            SqlCommandBuilder myBuilder = new SqlCommandBuilder(adapter);

            dgvProductInfo.DataSource = dt;

            adapter.UpdateCommand = myBuilder.GetUpdateCommand();
            adapter.Update(dt);

            dt.Clear();
            adapter.Fill(dt);
        }
    }

Your code has several problems: 您的代码有几个问题:

  1. You are not enclosing the SQLConnection in a using statement. 您没有将SQLConnection包含在using语句中。 You should always use this pattern so that the connection is properly disposed: using(var connection = new SqlConnection(...)){ rest of statements here } 您应该始终使用此模式,以便正确处理连接: using(var connection = new SqlConnection(...)){ rest of statements here }

  2. On the btnSave_Click function, you are getting the data again from the database -see the select * from tbl... code- and you are populating the DataTable again; btnSave_Click函数上,您将再次从数据库中获取数据-请btnSave_Click select * from tbl...代码-并再次填充DataTable。 therefore, it's obvious that there won't be any changes reflected. 因此,很明显不会有任何变化。 What you need to do instead is read the Updated DataTable from the page and use it inside the btnSave_Click function to push the updates to the database. 您需要做的是从页面中读取更新的数据表,并在btnSave_Click函数中使用它来将更新推送到数据库。

  3. Your question refers to DataGridView which I believe is a User Control on WinForms , but your question is tagged as ASP.NET . 您的问题涉及DataGridView ,我认为它是WinForms上的用户控件,但您的问题被标记为ASP.NET Are you referring to a GridView control instead? 您是在引用GridView控件吗? Either way, both controls should have a way of getting the DataSource bound to them. 无论哪种方式,两个控件都应具有将DataSource绑定到它们的方法。 You should be able to use that DataSource to push the updates to the database. 您应该能够使用该DataSource将更新推送到数据库。

I hope this pointers are helpful. 我希望这些指针对您有所帮助。 I cannot be more specific given the code you provided in your question. 考虑到您在问题中提供的代码,我无法更具体地说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM