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

前端 未结 2 1898
抹茶落季
抹茶落季 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

    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. 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.

    Jia Zhuang

提交回复
热议问题