如何从datagridview删除数据没有更新数据库

问题描述:

你好我在第一种形式有2个表格我有一个ADD,DELETE进程和datagridview我完成了从数据库删除数据没有更新数据库但是当我转到其他表格并回到我的第一个表格时数据仍然是在那里我不知道如何解决它感谢



这是我在删除按钮中的代码。

hello i have 2 forms in 1st form i have a ADD,DELETE process with a datagridview i done deleting data from database w/o updating the database but when i go to other form and back to my 1st form the data was still there i dont know how to fix it thanks

this is my code in delete button.

private void btnCdelete_Click(object sender, EventArgs e)
{
      int selectedIndex = DGV_Client.CurrentCell.RowIndex;
      if (selectedIndex > 0)
      {
          DGV_Client.Rows.RemoveAt(selectedIndex);
      }
}

您可以在c#中使用静态数据表来与datagridview绑定数据。在您的情况下,您只删除行而不是数据,所以当您从一个页面返回到另一个页面时,您的网格将在页面加载时再次绑定,它将绑定数据库中的数据。





而不是这样做获取静态数据表将帮助您在从一个页面导航到另一个页面期间维护行的值。所以当你从gridview中删除一行时也会从datatable中删除。

但是当您使用静态变量时,请注意您拥有大量数据。它将占用大量的记忆..
well you can use static datatable in c# to bind data with the datagridview. in your case you are deleteing rows only and not the data so when you come back from one page to other your grid will bind again on the page load and it will bind data from the database.


instead of doing this get static datatable will help you to maintain the values of rows during the navigation from one page to other. so when you delete a row from gridview also delete from datatable.
however be care full when you use static variable incase you have a large amount of data. it will occupy a lot of memory..