zoukankan      html  css  js  c++  java
  • C# WinForm listView 多行删除 操作

    //Button Delete , delete selected items from database
    
    private void buttonDelete_Click(object sender, EventArgs e)
    {
        DialogResult dr = MessageBox.Show("Are you sure to delete selected items? ", this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
        if (dr == DialogResult.OK)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();                               
    
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        string sql = "";
                        string id = "";
                        foreach (ListViewItem item in this.listView1.SelectedItems)
                        {
                            id = item.SubItems[0].Text.Trim();// 0 is the index of id column in listView1
                            sql = string.Format("delete from users where id='{0}'", id); 
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = sql;
                            cmd.ExecuteNonQuery();
                        }
                    }
    
                    FreshData(conn);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text);
            }
        }
    }
    
     
    
    //Button Remove, remove selected items from listView1
    private void buttonRemove_Click(object sender, EventArgs e)
    {
        foreach (ListViewItem item in this.listView1.SelectedItems)
        {
            if (item.Selected)
            {
                item.Remove();
            }
        }
        this.listView1.Refresh();
    }
    
     
    
    //Function Fresh Data, write by your self
    
    private void FreshData(SqlConnection conn)
    {
    }
  • 相关阅读:
    C# WebBrowser屏蔽alert的方法
    C# webbrowser实现真正意义上的F5刷新
    用于验证码图片识别的类(C#源码)
    递归
    排列组合数
    八皇后问题
    算24
    素数环
    acm
    qsort
  • 原文地址:https://www.cnblogs.com/tskin/p/4096454.html
Copyright © 2011-2022 走看看