[英]C# DataGridView (populated using a DataSource) not saving to Database
I have a form which I want to serve the purpose that the user can remove and edit (not add) rows from a database table, "Users". 我有一个表格,我希望达到这样的目的:用户可以从数据库表“用户”中删除和编辑(而不是添加)行。 I have created a form and a DataSource in VS2010, the DataSource was created using the new DataSource wizard. 我在VS2010中创建了一个表单和一个数据源,该数据源是使用新的数据源向导创建的。 From this I dragged and dropped the DataGridView for the Users table in the DataSource windowstrip to the form. 由此,我将DataSource窗口条中的Users表的DataGridView拖放到了表单上。
The issue I have is that when I run the application, the data will load into the DataGridView fine, but when i delete or edit a row and click save it does not update the database. 我的问题是,当我运行应用程序时,数据会很好地加载到DataGridView中,但是当我删除或编辑一行并单击“保存”时,它不会更新数据库。
I am an novice user so I'm sure I doing something stupid or naive - do i need to add some sql calls in here? 我是新手用户,所以我确定自己做的有些愚蠢或天真-我需要在此处添加一些sql调用吗?
Any ideas? 有任何想法吗?
public partial class EditUsers : Form
{
public EditUsers()
{
InitializeComponent();
}
private void EditUsers_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'debenhamsProjectOfficeDatabaseDataSet.Users' table. You can move, or remove it, as needed.
this.usersTableAdapter.Fill(this.debenhamsProjectOfficeDatabaseDataSet.Users);
}
private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.usersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.debenhamsProjectOfficeDatabaseDataSet);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
A much more flexible way to populate your datagrid view with your database is to use a connection string. 用数据库填充数据网格视图的一种更加灵活的方法是使用连接字符串。
Right click on your project file in solution explorer and add an new item. 在解决方案资源管理器中右键单击您的项目文件,然后添加一个新项目。 Add a class and name it connection.cs. 添加一个类并将其命名为connection.cs。 In here type 在这里输入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Your_Project_Name
{
class Connection
{
string ConnectionString;
public Connection()
{
ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:/User/somefolder/Your_Database.accdb;Persist Security Info=False;";
}
public string getConnection()
{
return ConnectionString;
}
}
Now add using System.Data.OleDb;
现在使用System.Data.OleDb;
添加System.Data.OleDb;
to your headers at the top of your form1. 到表格1顶部的标题。
Now in public partial class EditUsers : Form
put 现在在public partial class EditUsers : Form
放置public partial class EditUsers : Form
OleDbConnection connect = new OleDbConnection();
OleDbCommand command = new OleDbCommand();
OleDbDataReader reader;
Connection c;
Finally in private void EditUsers_Load
type in 最后在private void EditUsers_Load
键入
c = new Connection();
connect.ConnectionString = c.getConnection();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.