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

前端 未结 2 1897
抹茶落季
抹茶落季 2021-01-13 15:42

I\'m writing a Winforms app in C# that enables the user to edit and update database using the datagridview.

The problem is, I can\'t make it to work. The only thing

2条回答
  •  借酒劲吻你
    2021-01-13 16:25

    Without calling bindingSource1.EndEdit your underlying DataTable doesn't see any change and thus the Update command has nothing to 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);
    }
    

提交回复
热议问题