在windows桌面应用程序表单中,我使用此代码从datagridview和数据库中删除数据,我在dataridview中选中了一个复选框列,如果我点击checkbox
行,那时会从datagridview中删除,但是不是从数据库,所以当我重新加载表格时,我可以再次看到那行,我出错了?
public partial class EditEngClgList : Form
{
private OleDbConnection acccon = null;
private OleDbDataAdapter da = null;
private DataTable dt = null;
private BindingSource bs = null;
private OleDbCommandBuilder cmdb = null;
public EditEngClgList()
{
InitializeComponent();
try
{
acccon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb");
acccon.Open();
}
catch (Exception err)
{
MessageBox.Show("Error:" + err);
}
string sql = "Select * From EngColeges order by EngClgID";
da = new OleDbDataAdapter(sql, acccon);
cmdb = new OleDbCommandBuilder(da);
dt = new DataTable();
da.Fill(dt);
bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.DataSource = bs;
dataGridView1.Columns[1].Visible = false;
dataGridView1.Columns[2].HeaderText = "Engineering College Name";
dataGridView1.Columns[3].HeaderText = "Adress";
dataGridView1.Columns[4].HeaderText = "Entrance Type";
dataGridView1.Columns[2].Width = 400;
}
private void button4_Click(object sender, EventArgs e)
{
List<int> checkedclg = new List<int>();
DataRow dr;
List<int> checkedclgid = new List<int>();
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Delete"].Value) == true)
{
checkedclg.Add(i);
checkedclgid.Add(Convert.ToInt16(dataGridView1.Rows[i].Cells["Delete"].Value));
}
}
foreach (int k in checkedclg)
{
dr = dt.Rows[k];
dt.Rows[k].Delete();
foreach (int j in checkedclgid)
{
OleDbCommand oleDbCommand = new OleDbCommand("DELETE FROM EngColeges WHERE EngClgID = @clgID", acccon);
oleDbCommand.Parameters.Add("@clgID", OleDbType.Integer).Value = j;
oleDbCommand.Prepare();
oleDbCommand.ExecuteNonQuery();
}
}
}
答案 0 :(得分:1)
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Delete"].Value) == true)
{
checkedclg.Add(i);
checkedclgid.Add(Convert.ToInt16(dataGridView1.Rows[i].Cells["Delete"].Value));
}
看起来错误的单元格值传递给Convert.ToInt16
?它使用“已删除”列而不是您的ID列。
您还可以使用where in
子句删除一个sql语句中的所有行,例如:
DELETE FROM table WHERE id IN (1, 2, 3, 4)
答案 1 :(得分:1)
而不是像这样存储Delete
的值
checkedclgid.Add(Convert.ToInt16(dataGridView1.Rows[i].Cells["Delete"].Value));
存储primary key column
这样的值会从数据库中正确删除数据
checkedclgid.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells["EngClgID"].Value));
答案 2 :(得分:0)
我认为首先要删除该行,然后尝试将其从数据库中删除。首先,从数据库中删除该行,然后再次调用该表的select查询。
答案 3 :(得分:0)
你现在所拥有的只是command
。您需要OleDbDataAdapter
,并将command
传递给它:
...
da = new OleDbDataAdapter(sql, acccon);
da.DeleteCommand = oleDbCommand;
更多信息:http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.deletecommand.aspx
答案 4 :(得分:0)
只需替换这两行:
oleDbCommand.Prepare();
oleDbCommand.ExecuteNonQuery();
使用:
da.DeleteCommand = oleDbCommand;
参考文献: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdataadapter.deletecommand.aspx
答案 5 :(得分:0)
您必须更改此行:
checkedclgid.Add(Convert.ToInt16(dataGridView1.Rows[i].Cells["Delete"].Value));
以下内容:
checkedclgid.Add(Convert.ToInt32(dataGridView1.Rows[i].Cells["CellInTheInvisibleColumn"].Value));
因为您已将布尔类型转换为Int16,然后您在查询中使用它来检查相关表中的ID。
OleDbCommand oleDbCommand = new OleDbCommand("DELETE FROM EngColeges WHERE EngClgID = @clgID", acccon);
因此,您必须存储要删除它们的行的ID。