码迷,mamicode.com
首页 > Windows程序 > 详细

WinForm DataGridView根据选中的复选框删除

时间:2016-11-24 16:03:19      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:edit   check   form data   tag   void   ted   选择   new   datagrid   

     注意:在DataGridView添加一列(name:delete),ColumnType属性为:DataGridViewCheckBoxColumn,FlaseValue属性为:Flase,TureValue属性为Ture,并设置DataGridView的ReadOnly属性为Flase。

 

 

  1. //删除  
  2. private void del_button_Click(object sender, EventArgs e)  
  3. { 
  4.      try  
  5.     {  
  6.          int count = 0;  
  7.          for (int i = 0; i < form_dataGridView.RowCount; i++)  
  8.         {  
  9.              if (form_dataGridView.Rows[i].Cells[8].EditedFormattedValue.ToString() == "True")  
  10.             {  
  11.                 count++;  
  12.             }  
  13.         }  
  14.         if (count == 0)  
  15.         {  
  16.               MessageBox.Show("请至少选择一条数据!", "提示");  
  17.               return;  
  18.         }  
  19.        else  
  20.         {  
  21.             if (MessageBox.Show(this, "共选择" + count + "条,你要删除这些数据吗?", "提         示", MessageBoxButtons.YesNo, MessageBoxIcon.Information).ToString() == "Yes")  
  22.             {  
  23.                   string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();  
  24.                   OleDbConnection conn = new OleDbConnection(connStr);  
  25.                   conn.Open();  
  26.                   string str = "(";  
  27.                   for (int i = 0; i < form_dataGridView.RowCount; i++)  
  28.                  {  
  29.                       if (form_dataGridView.Rows[i].Cells[8].EditedFormattedValue.ToString() == "True")  
  30.                      {  
  31.                          str += form_dataGridView.Rows[i].Cells[0].Value.ToString() + ",";  
  32.                      }  
  33.                 }  
  34.                 if (str.Length > 0)  
  35.                 {  
  36.                      str = str.Substring(0, str.Length - 1);  
  37.                      str += ")";  
  38.                      MessageBox.Show(str);  
  39.                 }  
  40.                 string sql = "delete from form where id in " + str;  
  41.                 OleDbCommand cmd = new OleDbCommand(sql, conn);  
  42.                 cmd.ExecuteNonQuery();  
  43.                 conn.Close();  
  44.                 MessageBox.Show("删除成功");  
  45.                 GetData();//引用事件,刷新数据  
  46.             }  
  47.            else  
  48.             {  
  49.                   return;  
  50.             }  
  51.         }  
  52.     }  
  53.     catch  
  54.     {  
  55.     }  
  56. } 

WinForm DataGridView根据选中的复选框删除

标签:edit   check   form data   tag   void   ted   选择   new   datagrid   

原文地址:http://www.cnblogs.com/ZHANGKAIXUAN/p/6097505.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!