【发布时间】:2016-12-12 08:08:22
【问题描述】:
我有一个使用我的数据库中的数据集填充的 DataGridView。我正在尝试在 DataGridView 中进行更改,并将这些更改应用到数据库,只要我按“Enter”。
我阅读了很多同样的问题,并研究了该主题,但仍然无法弄清楚为什么我无法将 DataGridView 中所做的更改应用于我的数据库。 (我知道之前有人问过这个问题,但仍然无法弄清楚)。
谁能告诉我我做错了什么?
DataSet ds = new DataSet();
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlDataAdapter da;
public ListForm()
{
//Setting up DataGridView with data.
InitializeComponent();
da = new SqlDataAdapter("Select * from Contact_List", constring);
SqlCommandBuilder cmb = new SqlCommandBuilder(da);
da.UpdateCommand = cmb.GetUpdateCommand();
da.Fill(ds, "Contact_List");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Contact_List";
}
//Trying to update database with DataAdapter
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
//I believe that the changes to the database should be applied here
//Buts its not working
da.Update(ds, "Contact_List",);
ds.AcceptChanges();
con.Close();
}
}
【问题讨论】:
-
您应该在尝试保存更改之前结束当前编辑。
标签: c# sql-server winforms datagridview ado.net