我想删除数据gridview行 [英] I want to delete data gridview row

查看:109
本文介绍了我想删除数据gridview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,if i delete a single row in the data grid view mean that deleted row  will remove from the grid view, but i have this error "ExecuteNonQuery: CommandText property has not been initialized"  





我尝试过:





What I have tried:

       string update="";
       DateTime now = DateTime.Now;
       private void delete()
       {
           try
           {
               if ((gvClass.Rows.Count > 0) || gvClass.CurrentCell.ColumnIndex < 1)
               {
                   search = Convert.ToString(gvClass.CurrentRow.Cells[0].Value.ToString());
               }
               if (MessageBox.Show("Do you want to Delete?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
               {
               }
               else
               {
                   con.Open();
                   string query1 = "select * from tblClass  WHERE  ID LIKE '" + search + "' and status=1  order by ID ";
                   SqlCommand cmd1 = new SqlCommand(query1, con);
                   SqlDataReader dr1 = cmd1.ExecuteReader();
                   if (dr1.Read())
                   {
                       string f = dr1["ID"].ToString();
                       if (search == f)
                       {
                          update    = "update tblClass set status=0,delete_time=convert(datetime,'" + now + "', 103) where  ID='" + search + "' ";
                       }
                   }

                   dr1.Close();
                   cmd1.ExecuteNonQuery();

                   con.Close();

                   con.Open();
                   cmd = new SqlCommand(update, con);
                   cmd.ExecuteNonQuery();
                   con.Close();
                   MessageBox.Show("Deleted SuccessFully");
                   view();


               }
           }
           catch (Exception ex)
           {
               throw ex;

           }

       }


private void view()
       {
           string view ="";
           con.Open();
           SqlDataAdapter da =new SqlDataAdapter("select * from tblClass where status =1",con);
           DataSet ds = new DataSet ();
           this.gvClass.Rows.Clear();
           gvClass.Rows.Clear();
           da.Fill(ds);
           DataTable dt =ds.Tables[0];

           if (dt.Rows.Count != 0)
           {
               gvClass.Rows.Add(dt.Rows.Count);
               for (int i = 0; i < dt.Rows.Count; i++)
               {
                   for (int i1 = 0; i1 < gvClass.Rows.Count - 0; i1++)
                   {
                       gvClass.Rows[i1].Cells[0].Value = (i1 + 1).ToString();
                   }
                   gvClass.Rows[i].Cells["Id"].Value = Convert.ToString(dt.Rows[i]["Id"].ToString());
                   gvClass.Rows[i].Cells["Name"].Value = Convert.ToString(dt.Rows[i]["Name"].ToString());

               }
           }
               con.Close();

           }

推荐答案

我希望错误将出现在这组代码上/>
I hope the error will be on this set of codes
Quote:

if(search == f)

{

update =update tblClass set status = 0,delete_time = convert(datetime,'+ now +',103)其中ID ='+ search +';

} < br $>
}

dr1.Close();

cmd1.ExecuteNonQuery();

con.Close();

con.Open();

cmd = new SqlCommand(update,con);

cmd.ExecuteNonQuery();

if (search == f)
{
update = "update tblClass set status=0,delete_time=convert(datetime,'" + now + "', 103) where ID='" + search + "' ";
}
}
dr1.Close();
cmd1.ExecuteNonQuery();
con.Close();
con.Open();
cmd = new SqlCommand(update, con);
cmd.ExecuteNonQuery();



_______________________________________________________________________________________



如果条件(搜索== f)不满意然后更新变量将为空

所以当你尝试执行一个空字符串时它会导致你在帖子中提到的错误。



移动条件内的所有代码并尝试。



警告:永远不要连接sql语句,它会导致 sql injectio [ ^ ] n攻击

总是使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]


_______________________________________________________________________________________

If the Condition (search == f) doesnt get satisfy then the update variable will be empty
so when you try to execute an empty string it will lead to the error you have mentioned in the post.

Move all the codes inside the condition and try.

caution: Never concatenate the sql statements, it will lead to sql injectio[^]n attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]


If you have AllowUserToAddRows property enabled on your DataGridView then you might be accidently deleting the empty row at the bottom of the DataView which is a placeholder for the next user created row. Try disabling this option if not required, otherwise try using code like this.




foreach (DataGridViewRow row in dataGridView1.SelectedRows)
{
    if(!row.IsNewRow)
       dataGridView1.Rows.Remove(row);
}




SqlConnection con = new SqlConnection(ConString);
//create and initialise connection object properly.




open and close SQL connection and DataReader object properly.




if ((rdr != null))
                            { rdr.Close(); }

                            if (con.State == ConnectionState.Open)
                            { con.Close(); }




carefully look at the exception and your logic. and CommandText property initialized properly.


这篇关于我想删除数据gridview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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