为什么不使用DataGridView更新数据库? [英] Why wont DataGridView Update Database?

查看:85
本文介绍了为什么不使用DataGridView更新数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,其中填充了数据库中的数据集。我试图在DataGridView中进行更改,并在每次按Enter键时将这些更改应用于数据库。

I have a DataGridView populated with a DataSet from my Database. I am trying to make changes in the DataGridView and apply those changes to the Database whenever I press 'Enter'.

Iv大量阅读了相同的问题,并研究了该主题,但仍然无法弄清楚为什么我无法将对DataGridView所做的更改应用于数据库。 (我知道这已经被问过了,但仍然无法解决)。

Iv read alot of this same question, and researched the topic, but am still having trouble figuring out why I cannot apply changes made in my DataGridView to my Database. (I know this has been asked before, but still cant figure this out).

有人可以告诉我我在做什么错吗?

Can anyone show me what im doing wrong?

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();
    }  
}


推荐答案

您应该在尝试保存更改之前结束当前编辑:

You should end current edit before trying to save changes:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    var cm = dataGridView1.BindingContext[ds, "Contact_List"];
    cm.EndCurrentEdit();
    da.Update(ds, "Contact_List");
}

一些其他音符


  • 您不需要 DataSet DataTable 就足够了。

  • 当您通过传递一个 SqlCommandBuilder 时, SqlDataAdapter 到构造函数,所有插入,更新和删除命令将自动生成,您不需要自己做任何事情,因此 GetUpdateCommand() 是不必要的。

  • 调用 EndCurrentEdit 导致您在 IEditableObject 保存到基础数据源。 DataRowView 是网格行后面的对象,它是 IEditableObject ,您应该调用 EndCurrentEdit <导致 DataRowView EndEdit 的货币管理器的/ code>被调用并将更改提交给基础 DataRow 并结束编辑会话。

  • 如果将网格绑定到 BindingSource ,调用 BindingSource EndEdit 会执行相同的操作。

  • 保存后数据,您无需手动调用 AcceptChanges

  • 您需要在代码中添加异常处理。

  • You don't need a DataSet. A DataTable is enough.
  • When you create a SqlCommandBuilder by passing a SqlDataAdapter to the constructor, all insert, update and delete commands will be generated automatically and you don;t need to do anything yourself, so GetUpdateCommand() is not necessary.
  • Calling EndCurrentEdit cause the changes which you made on an IEditableObject be saved to underlying data source. DataRowView which is the object behind rows of grid is an IEditableObject and you should call EndCurrentEdit of the currency manager which cause EndEdit of DataRowView be called and commits changes to the underlying DataRow and ends the editing session.
  • If you bind the grid to a BindingSource, calling EndEdit of BindingSource will do the same.
  • After saving data, you don't need to call AcceptChanges manually.
  • You need to add exception handling to code.

这篇关于为什么不使用DataGridView更新数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆