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

C# WinForm listView 多行删除 操作

时间:2014-11-14 10:34:26      阅读:434      评论:0      收藏:0      [点我收藏+]

标签:winform   style   blog   io   color   ar   os   sp   for   

//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# WinForm listView 多行删除 操作

标签:winform   style   blog   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/tskin/p/4096454.html

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