怎么了?我无法通过单击按钮删除该行 [英] Whats wrong? I can not delete the row with button click

查看:57
本文介绍了怎么了?我无法通过单击按钮删除该行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button4_Click(object sender, EventArgs e)
        {
            if (bar_code1DataGridView.SelectedRows.Count > 0)
            {

                DataGridViewRow dr = bar_code1DataGridView.SelectedRows[0];
                if (!string.IsNullOrEmpty(dr.Cells["dataGridViewTextBoxColumn1"].Value.ToString()))
                {

                    String cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=barcode;Integrated Security=True";
                    using (SqlConnection openCon = new SqlConnection(cs))
                    {


                        using (SqlCommand querySaveStaff = new SqlCommand("DELETE FROM bar_code1 WHERE id=@id", openCon))
                        {

                            // querySaveStaff.Parameters.Add("@id",SqlDbType.Int).Value=idTextBox.Text;
                            querySaveStaff.Parameters.AddWithValue("@id", 0);
                            
                            openCon.Open();
                            querySaveStaff.ExecuteNonQuery();
                            openCon.Close();

                        }

                    }

                    using (SqlConnection openCon = new SqlConnection(cs))

                    {

                        string saveStaff = "SELECT * FROM dbo.bar_code1 ";
                        openCon.Open();
                        using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))
                        {
                            querySaveStaff.Fill(barcodeDataSet.bar_code1);
                            bar_code1DataGridView.DataSource = barcodeDataSet.bar_code1;
                            bar_code1DataGridView.Update();
                            bar_code1DataGridView.Refresh();
                        }

                    }
                }
            }
        }<pre lang="text">





我尝试了什么:





What I have tried:

Whats wrong? I can not delete the row with button click

推荐答案

可能是您的ID值已修复:

Probably, it's that your ID value is fixed:
using (SqlCommand querySaveStaff = new SqlCommand("DELETE FROM bar_code1 WHERE id=@id", openCon))
    {
    querySaveStaff.Parameters.AddWithValue("@id", 0);

一旦删除该行,就无法再删除它。

至于您注释掉的代码:

Once you delete that row, you can't delete it again.
And as for your commented out code:

querySaveStaff.Parameters.Add("@id",SqlDbType.Int).Value=idTextBox.Text;

这也不一定有效。

而是使用方法顶部的int.TryParse来检查和验证用户输入,然后使用转换后的值作为参数。

That's not necessarily going to work either.
Instead, use int.TryParse at the top of your method to check and validate the user input, then use the converted value as the parameter.

int id;
if (!int.TryParse(idTextBox.Text, out id))
   {
   ... report problem to user ... 
   return;
   }
...
querySaveStaff.Parameters.AddWithValue("@id", id);



但请整理你的变量名! querySaveStaff删除数据?没有明显的自我文档!


But please, sort out your variable names! "querySaveStaff" deleting data? Not an obvious bit of self documentation!


private void button4_Click(object sender, EventArgs e)
       {
           int id;
           if (bar_code1DataGridView.SelectedRows.Count > 0)
           {

               DataGridViewRow dr = bar_code1DataGridView.SelectedRows[0];
               if (!int.TryParse(idTextBox.Text, out id))
               {

               String cs = "Data Source=.\\SQLEXPRESS;Initial Catalog=barcode;Integrated Security=True";
                   using (SqlConnection openCon = new SqlConnection(cs))
                   {


                       using (SqlCommand querySaveStaff = new SqlCommand("DELETE FROM bar_code1 WHERE id=@id", openCon))
                       {

                           // querySaveStaff.Parameters.Add("@id",SqlDbType.Int).Value=idTextBox.Text;
                           querySaveStaff.Parameters.AddWithValue("@id", id);

                           openCon.Open();
                           querySaveStaff.ExecuteNonQuery();
                           openCon.Close();

                       }




               }



               using (SqlConnection openCon = new SqlConnection(cs))

                   {

                       string saveStaff = "SELECT * FROM dbo.bar_code1 ";
                       openCon.Open();
                       using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, cs))
                       {
                           querySaveStaff.Fill(barcodeDataSet.bar_code1);
                           bar_code1DataGridView.DataSource = barcodeDataSet.bar_code1;
                           bar_code1DataGridView.Update();
                           bar_code1DataGridView.Refresh();
                       }

                   }
               }


                       MessageBox.Show("Dont Work ");
                   return;


           }
       }


这篇关于怎么了?我无法通过单击按钮删除该行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆