【问题标题】:Delete datagridview cell click event删除datagridview单元格点击事件
【发布时间】: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 中删除”,但我得到的结果还是一样

标签: c# sql


【解决方案1】:

试试这个:

        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);
        }

【讨论】:

  • 你好。我正在使用您共享的代码,但我得到的结果与以前相同
  • 嗨 Johh,在这种情况下,请检查 ExecuteNonQuery() 的值,如果它是 -1 则表示不成功。尝试从 SQL 查询中删除相同的内容以检查是否收到任何约束错误。
【解决方案2】:

一开始,你必须打开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语句并使用要释放的资源。有关这方面的更多信息,请查看this

话虽如此,建议的方法如下:

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;  
       }
    }
}

【讨论】:

  • 嗨,克里斯托斯先生。我使用了您共享的代码,即代码 #1 和 #2。它对我有用,但我仍然得到了和以前一样的结果,可悲的是
  • @JohnSanityCOPH 嗨!一个问题,能否请您在dataGridView1.DataSource = dt; 放置一个断点并验证数据是否从数据库中加载?如果它们已加载,那么我们应该在其他地方寻找问题的解决方案。否则,我们没有数据,我们无法判断。谢谢
  • 嗨@Christos!我现在已经找到问题并解决了。问题是在我的数据库中,“rfidno”的数据类型是 varchar,我在将 int 更改为它解决的字符串时输入的是“int”。感谢您的评论它解决了我的问题
  • 嗨@JohnSanityCOPH!听起来不错。你找到了 :) 赞。
猜你喜欢
  • 2012-09-27
  • 2015-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
相关资源
最近更新 更多