将从Datagridview输入的新记录保存到数据库中 [英] save new record entered from Datagridview into the database

查看:371
本文介绍了将从Datagridview输入的新记录保存到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用DataSet和TableAdapter来填充Datagridview。 Datagridview允许在底部插入新记录。现在,当我在Datagridview中为新记录输入值时,记录不会自动保存回数据库。我需要处理哪个事件或者需要写什么代码才能将新记录保存回数据库。



我使用C#。

$您是在 TableAdapter上调用 Update()方法的方法

?以下代码来自 MSDN对 TableAdapter

  try 
{$ b $ varidate();
this.customersBindingSource.EndEdit();

this.customersTableAdapter.Update(this.northwindDataSet.Customers);
MessageBox.Show(Update successful);
}
catch(System.Exception ex)
{
MessageBox.Show(Update failed);
}

有很多地方你可以把这种逻辑 - 建议您可以在表单上具有保存按钮,或者如果您希望在输入每行时保存数据,则可以使用RowValidated事件处理程序来保存逻辑。


Am using a DataSet and a TableAdapter to populate a Datagridview. The Datagridview allows inserting new records at the bottom. Now when I enter values for the new record in the Datagridview, the record is not automatically saved back to the database. Which event do U need to handle or what code do I need to write to get the new record saved back to the databse.

am using C#.

解决方案

Are you calling the Update() method on the TableAdapter? The following code comes from MSDN's discussion on the TableAdapter:

try 
{
    this.Validate();
    this.customersBindingSource.EndEdit();

    this.customersTableAdapter.Update(this.northwindDataSet.Customers);
    MessageBox.Show("Update successful"); 
} 
catch (System.Exception ex) 
{
    MessageBox.Show("Update failed"); 
}

There are various places where you can put this sort of logic - as Davide Piras suggests you could have a Save button on your form or if you want data to be saves as each row is entered you can have your save logic with the RowValidated event handler.

这篇关于将从Datagridview输入的新记录保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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