首页 > 代码库 > C# WinForm listView 多行删除 操作
C# WinForm listView 多行删除 操作
//Button Delete , delete selected items from databaseprivate 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 listView1private 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 selfprivate void FreshData(SqlConnection conn){}
C# WinForm listView 多行删除 操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。