右键

ContextMenustrip

 

删除行

DataGridview1.Rows.RemoveAt(行索引值);

 

private void Form1_Load(object sender, EventArgs e)
        {
            string constr = "server=192.168.100.222;uid=sa;pwd=p@ssw1rd;database=pwd1";
            SqlConnection mycon = new SqlConnection(constr);
            DataTable mytb = new System.Data.DataTable();
            mycon.Open();
            SqlDataAdapter mydapt = new SqlDataAdapter("select * from book",mycon);
            mydapt.Fill(mytb);

            dataGridView1.DataSource = mytb;

        }
        //鼠标键抬起时
        privatevoiddataGridView1_CellMouseUp(objectsender,DataGridViewCellMouseEventArgs e)
        {
            if (e.Button==MouseButtons.Right)
            {
                //右键选择当前行
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
                //当前单元格唯一
                this.dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
                //显示右键菜单(在datagridview中的e.location位置显示)
                this.contextMenuStrip1.Show(dataGridView1,e.Location);
                //显示右键菜单在鼠标的位置显示
                this.contextMenuStrip1.Show(Cursor.Position);
            }
            
        }

 

右键删除行.
        private void Form1_Load(object sender, EventArgs e)
        {
            string constr = "server=192.168.100.222;uid=sa;pwd=p@ssw1rd;database=pwd1";
            SqlConnection mycon = new SqlConnection(constr);
            DataTable mytb = new System.Data.DataTable();
            mycon.Open();
            SqlDataAdapter mydapt = new SqlDataAdapter("select * from book",mycon);
            mydapt.Fill(mytb);

            dataGridView1.DataSource = mytb;

        }
        //鼠标键抬起时
        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button==MouseButtons.Right)
	        {
                //右键选择当前行
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
                //当前单元格唯一
                this.dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
                //显示右键菜单(在datagridview中的e.location位置显示)
                this.contextMenuStrip1.Show(dataGridView1,e.Location);
                //显示右键菜单在鼠标的位置显示
                this.contextMenuStrip1.Show(Cursor.Position);
	        }
            
        }

 

相关文章:

  • 2021-12-26
  • 2021-07-10
  • 2021-09-01
  • 2021-05-16
  • 2022-01-02
  • 2021-06-17
  • 2021-06-04
  • 2022-12-23
猜你喜欢
  • 2021-12-01
  • 2022-12-23
  • 2021-08-17
  • 2021-11-20
  • 2021-07-11
  • 2022-01-28
  • 2021-12-06
相关资源
相似解决方案