简体   繁体   English

如何使用 Datagridview 绑定源 C# 更新 SQL Server 数据库

[英]How to update SQL Server database using Datagridview binding source C#

I'm writing a Winforms app in C# that enables the user to edit and update database using the datagridview.我正在用 C# 编写一个 Winforms 应用程序,使用户能够使用 datagridview 编辑和更新数据库。

The problem is, I can't make it to work.问题是,我无法让它工作。 The only thing that I managed to achieve is updating what the datagridview is showing, but when I go into the database table, there is no change in the data.我设法实现的唯一一件事是更新 datagridview 显示的内容,但是当我进入数据库表时,数据没有变化。 I have search a lot of sites discussing about that problem but nothing yet worked for me.我搜索了很多讨论这个问题的网站,但还没有对我有用。

Things I have tried so far:到目前为止我尝试过的事情:

  • binding the database to the datagridview将数据库绑定到 datagridview
  • changing the Copy to Output Directory property to Copy if newerCopy to Output Directory属性更改为Copy if newer
  • using the dataAdapter.Update((DataTable)bindingSource1.DataSource) with or without any update query execute.使用dataAdapter.Update((DataTable)bindingSource1.DataSource)执行或不执行任何更新查询。
  • execute update query without dataAdapter.update(...) .在没有dataAdapter.update(...)的情况下执行更新查询。

Here is my code:这是我的代码:

public Form1()
{
    InitializeComponent();
    GetData("SELECT * FROM Table1");
}

void GetData(string selectCommand)
{
    SqlConnection conn = new SqlConnection(Properties.Settings.Default.NewDBTESTConnectionString);

    dataAdapter = new SqlDataAdapter(selectCommand, conn);
    commandBuilder = new SqlCommandBuilder(dataAdapter);

    table = new DataTable();
    dataAdapter.Fill(table);

    bindingSource1.DataSource = table;
    dataGridView1.DataSource = bindingSource1;
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
        dataAdapter.Update((DataTable)bindingSource1.DataSource);    
}

Without calling bindingSource1.EndEdit your underlying DataTable doesn't see any change and thus the Update command has nothing to update.如果不调用bindingSource1.EndEdit您的基础 DataTable 看不到任何更改,因此 Update 命令没有任何更新。

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    bindingSource1.EndEdit();
    DataTable dt = (DataTable)bindingSource1.DataSource;

    // Just for test.... Try this with or without the EndEdit....
    DataTable changedTable = dt.GetChanges();
    Console.WriteLine(changedTable.Rows.Count);

    int rowsUpdated = da.Update(dt);    
    Console.WriteLine(rowsUpdated);
}

You can have a save or update button to execute the code like this:您可以使用保存或更新按钮来执行如下代码:

bindingSource1.EndEdit();
DataTable dt = (DataTable)bindingSource1.DataSource;   
dataAdaper.Update(dt);    

You will save all changes you made, but if you sort datagridview columns first then change data then run the save code above you will miss one record, the top first one before you sort.您将保存所做的所有更改,但如果您先对 datagridview 列进行排序,然后更改数据,然后运行上面的保存代码,您将丢失一条记录,即排序前的第一个记录。 To fix it, you have to sort it back then save them.要修复它,您必须对其进行排序然后保存它们。 The best way to do it if you know which column to sort, you need to sort it when you load data to datagridview.如果您知道要对哪一列进行排序,最好的方法是在将数据加载到 datagridview 时对其进行排序。

Jia Zhuang贾庄

You need to recreate the entire SqlDataAdapter in the CellEndEdit event.您需要在 CellEndEdit 事件中重新创建整个 SqlDataAdapter。

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        var dataAdapter = new SqlDataAdapter("select * from [table]", conn);
        var commandBuilder = new SqlCommandBuilder(dataAdapter);
        dataAdapter.UpdateCommand = new SqlCommandBuilder(dataAdapter).GetUpdateCommand();            
        bindingSource1.EndEdit();
        dataAdapter.Update((DataTable)bindingSource1.DataSource);
    }

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

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