c#-无法使用 sqlite 数据库删除 datagridview 中的多行
c#- Can't delete multi rows in datagridview with sqlite database
我有一个数据网格视图,我在其中添加了一些列,第一列有复选框
当我尝试 select 带有复选框的多行并将其删除时,它每次只删除一行,即使我 select 多行。
我用来填充数据网格视图的代码
SQLiteDataAdapter da = new SQLiteDataAdapter("Select * From Data", con);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = "false";
dataGridView1.Rows[n].Cells[1].Value = item["id"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["car_num"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["customer"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["driver"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["first"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["second"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["sum"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["price"].ToString();
dataGridView1.Rows[n].Cells[9].Value = item["date1"].ToString();
dataGridView1.Rows[n].Cells[10].Value = item["date2"].ToString();
dataGridView1.Rows[n].Cells[11].Value = item["city"].ToString();
dataGridView1.Rows[n].Cells[12].Value = item["type"].ToString();
}
我用来删除的代码
private void bDelete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (bool.Parse(item.Cells[0].Value.ToString()))
{
if (MessageBox.Show("Are you sure you want to delete these data ? ", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
con.Open();
SQLiteCommand cmd = new SQLiteCommand("Delete From Data Where id='"+item.Cells[1].Value.ToString()+"'", con);
cmd.ExecuteNonQuery();
con.Close();
SQLiteDataAdapter da = new SQLiteDataAdapter("Select * From Data", con);
DataTable dt = new DataTable();
dataGridView1.Rows.Clear();
da.Fill(dt);
MessageBox.Show("Success");
}
}
}
我认为问题在于 foreach 循环内的 dataGridView1.Rows.Clear();
。由于 foreach 循环遍历 (DataGridViewRow item in dataGridView1.Rows)
,因此在清除行后就没有什么可以迭代的了。
我有一个数据网格视图,我在其中添加了一些列,第一列有复选框 当我尝试 select 带有复选框的多行并将其删除时,它每次只删除一行,即使我 select 多行。
我用来填充数据网格视图的代码
SQLiteDataAdapter da = new SQLiteDataAdapter("Select * From Data", con);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = "false";
dataGridView1.Rows[n].Cells[1].Value = item["id"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["car_num"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["customer"].ToString();
dataGridView1.Rows[n].Cells[4].Value = item["driver"].ToString();
dataGridView1.Rows[n].Cells[5].Value = item["first"].ToString();
dataGridView1.Rows[n].Cells[6].Value = item["second"].ToString();
dataGridView1.Rows[n].Cells[7].Value = item["sum"].ToString();
dataGridView1.Rows[n].Cells[8].Value = item["price"].ToString();
dataGridView1.Rows[n].Cells[9].Value = item["date1"].ToString();
dataGridView1.Rows[n].Cells[10].Value = item["date2"].ToString();
dataGridView1.Rows[n].Cells[11].Value = item["city"].ToString();
dataGridView1.Rows[n].Cells[12].Value = item["type"].ToString();
}
我用来删除的代码
private void bDelete_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in dataGridView1.Rows)
{
if (bool.Parse(item.Cells[0].Value.ToString()))
{
if (MessageBox.Show("Are you sure you want to delete these data ? ", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
{
con.Open();
SQLiteCommand cmd = new SQLiteCommand("Delete From Data Where id='"+item.Cells[1].Value.ToString()+"'", con);
cmd.ExecuteNonQuery();
con.Close();
SQLiteDataAdapter da = new SQLiteDataAdapter("Select * From Data", con);
DataTable dt = new DataTable();
dataGridView1.Rows.Clear();
da.Fill(dt);
MessageBox.Show("Success");
}
}
}
我认为问题在于 foreach 循环内的 dataGridView1.Rows.Clear();
。由于 foreach 循环遍历 (DataGridViewRow item in dataGridView1.Rows)
,因此在清除行后就没有什么可以迭代的了。