NetLover

导航

DataGridView 如何删除行

/// <summary>
/// 删除
/// </summary>

private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            #region Shift多行删除
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("确定要删除选中的员工信息吗?删除后将不可恢复!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Del();
                }
               
            }
            else
            {
                MessageBox.Show("请选择要删除的员工信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            #endregion
        }


        /// <summary>
        /// 删除的方法
        /// </summary>
        public void Del()
        {
            int k = this.dataGridView1.SelectedRows.Count;
            if (dataGridView1.Rows.Count > 0)
            {
                for (int i = k; i >= 1; i--)//从下往上删,避免沙漏效应
                {
                    string usercode = dataGridView1.SelectedRows[i - 1].Cells["UCode"].Value.ToString();
                    if(userbll.DelUser(usercode))   //从数据库执行删除操作
                    {
                    }
                    else
                    {
                        MessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    this.dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index);
                }
            }
            else
            {
                dataGridView1.Rows.Clear();
            }

        }

------------------------------------------------------------------

DataGridView 如何实现鼠标右键删除行:

首先在程序中添加一个ContexMenuStrip1控件,给该控件添加删除项,
把 DataGridView 的ContexMenuStrip属性项绑定为你所添加的ContexMenuStrip1控件,然后就是你自己编写程序来操作删除具体的东西了。

 

posted on 2012-04-26 09:17  NetLover  阅读(7182)  评论(0编辑  收藏  举报