GTX_AI 发表于 2020-4-19 19:25:14

DataGridView取消键盘Delete功能

DataGridView取消键盘Delete功能:
DataGridView对象的AllowUserToDeleteRows属性为 False 时,用户的行删除操作就被禁止了。

(1)可以通过ContextMenuStrip控件添加鼠标右键删除功能:(推荐)
(2)可以在dataGridView1_CellMouseUp添加以下程序:
            删除右键删除
            if (e.Button == MouseButtons.Right)//判断是否当前弹起的右键
            {
                // 删除前的用户确认。
                if (MessageBox.Show("确认要删除该行数据吗?", "删除确认",
                MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question) == DialogResult.OK)
                {
                  try
                  {
                        this.dataGridView1.Rows.Selected = true;//选中鼠标所在的当前行
                        //DataGridViewRow currentRow = this.dataGridView1.Rows;

                        // 删除对应的Region
                        string class_pos = this.dataGridView1.Rows.Cells["Class"].Value.ToString();   //获取单元格列名为‘Class’的值
                        string row_pos = this.dataGridView1.Rows.Cells["Row"].Value.ToString();   //获取单元格列名为‘Row’的值
                        string col_pos = this.dataGridView1.Rows.Cells["Col"].Value.ToString();   //获取单元格列名为‘Col’的值

                        // 删除行对应的region
                        int classMove = Convert.ToInt32(class_pos);   //缺陷的类别
                        double RowMove = Convert.ToDouble(row_pos);   //行坐标的中心值
                        double ColMove = Convert.ToDouble(col_pos);   //列坐标的中心值

                        lock (locker)
                        {
                            // 根据【RowMove, ColMove】删除xx
                            xx
                            // 先获取坐标信息,再删除dategrid
                            this.dataGridView1.Rows.RemoveAt(e.RowIndex);
                        }

                  }
                  catch { }
                }
            }
参考:【1】C#如何使用右键菜单【contextMenuStrip】











页: [1]
查看完整版本: DataGridView取消键盘Delete功能