[英]Delete datagridview cell click event
刪除 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;
}
```
嘗試這個:
try
{
SqlDataAdapter da = new SqlDataAdapter();
sqlcon.Open();
da.DeleteCommand = new SqlCommand("delete from 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);
}
最初,您必須打開 SqlConnection,然后執行您的命令,綁定結果,最后不要忘記關閉連接:
public void bindGrid()
{
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True");
con.Open()
SqlCommand cmd = new SqlCommand("Select * from tbl_registerStudent", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
為了您不記得關閉SqlConnection
,更好的方法是使用using
語句並讓編譯器為您生成此調用。
public void bindGrid()
{
// This should be normally placed in a configuration file and read it from there.
string connectionString = @"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True";
using(var sqlConnection = new SqlConnection(connectionString))
{
con.Open()
SqlCommand cmd = new SqlCommand("Select * from tbl_registerStudent", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
此外,由於SqlCommand類繼承自DbCommand ,后者實現了IDisposable接口,因此您應該記住,您應該再次using
語句並釋放已用資源。 有關這方面的更多信息,請查看此。
話雖如此,建議的方法如下:
public void bindGrid()
{
// This should be normally placed in a configuration file and read it from there.
string connectionString = @"Data Source=DESKTOP-EB4EK81\SQLEXPRESS;Initial Catalog=TACLC;Integrated Security=True";
using(var sqlConnection = new SqlConnection(connectionString))
{
con.Open()
using(var cmd = new SqlCommand("Select * from tbl_registerStudent", con))
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.