【发布时间】:2021-02-07 18:17:45
【问题描述】:
删除 datagridview 单元格单击事件它表示信息已被删除,但在单击弹出消息后,即使我删除了某些信息,该信息仍然存在。请帮忙
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string maincon = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
SqlConnection sqlcon = new SqlConnection(maincon);
int rfidno = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["rfidno"].FormattedValue.ToString());
try
{
SqlDataAdapter da = new SqlDataAdapter();
sqlcon.Open();
da.DeleteCommand = new SqlCommand("delete tbl_registerStudent where rfidno = '" + rfidno + "'", sqlcon);
da.DeleteCommand.ExecuteNonQuery();
MessageBox.Show("" + rfidno);
MessageBox.Show("Delete Successfull");
sqlcon.Close();
bindGrid();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
这是bindGrid的代码
public void bindGrid()
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * from tbl_registerStudent", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
```
【问题讨论】:
-
能否分享
bindGrid方法的代码? -
“信息还在”:你的意思是在数据库中还是在 UI 中?
-
您的查询是错误的“从 tbl_registerStudent 中删除”
-
你好@KlausGütter他们俩都是伙伴,点击单元格后信息仍然存在
-
嗨 @MuhammadWaqasAziz 我已经尝试过“从 tbl_registerStudent 中删除”,但我得到的结果还是一样