如何删除datagridview中的行,同时更新访问中的数据库 [英] How to delete rows in a datagridview and at the same time updating the database in access
问题描述
我正在使用C#开发一个应用程序,可以访问在MS Access 2007中创建的数据库。我有一个WindowsForm来删除数据库中的记录,但我只能删除datagriedview中的行(第一个除外)并且数据库没有更新。
当我尝试删除一行时出现此错误:
System.Data.OleDbException(0x80040E10):没有为一个或多个必需参数提供任何值。
如何解决这些问题:
当我在datagriedview中删除记录时数据库没有更新。
不要让我删除datagriedview中的第一行
提前致谢,
我的尝试:
I am developing an application in C # with access to a database created in MS Access 2007. I have a WindowsForm to delete records from the database, but I'm only able to delete rows in the datagriedview (except the first) and the database is not being updated.
I get this error when I try to delete a row:
System.Data.OleDbException (0x80040E10): There was provided no value to one or more required parameters.
How can I solve these problems:
The database is not updated when i delete a record in the datagriedview.
Don't let me delete the first line in datagriedview
Thanks in advance,
What I have tried:
private void btn_remover_Click(object sender, EventArgs e)
{
using (OleDbConnection oleConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Stock_Material.accdb"))
{
try
{
oleConn.Open();
if (MessageBox.Show("Tem a certeza que pretende remover este registo(s)", " Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
foreach (DataGridViewRow row in dataGridView_remover.SelectedRows)
{
int rowId = Convert.ToInt32(row.Cells[0].Value);
if (rowId > 0)
{
dataGridView_remover.Rows.RemoveAt(row.Index);
OleDbCommand delcmd = new OleDbCommand("Delete from product where id_Produto= " + rowId + "");
delcmd.Connection = oleConn;
delcmd.ExecuteNonQuery();
}
}
}
catch (Exception ex )
{
MessageBox.Show(ex.ToString());
}
oleConn.Close();
}
}
推荐答案
您可以删除所选行(s )从数据库中,然后重新获取数据,并刷新gridview ...
You could delete the selected row(s) from the database, THEN re-fetch the data, and refresh the gridview...
这篇关于如何删除datagridview中的行,同时更新访问中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!