15.5DataGridView右键删除当前行

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _15._5DataGridView右键删除行
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“csharpzxwDataSet.mytable001”中。您可以根据需要移动或删除它。
            this.mytable001TableAdapter.Fill(this.csharpzxwDataSet.mytable001);

        }
        private int rowIndex = 0;//定义一个全局变量,以便删除行方法可以访问的到
        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)//判断是否当前弹起的右键
            {
                rowIndex = e.RowIndex;
                this.dataGridView1.Rows[e.RowIndex].Selected = true;//选中鼠标所在的当前行
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];//默认当前单元格为第一行第一个
                //this.contextMenuStrip1.Show(this.dataGridView1, e.Location);//右键菜单绑定当前位置,也就是第一行第一个
                contextMenuStrip1.Show(Cursor.Position);
            }
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!this.dataGridView1.Rows[this.rowIndex].IsNewRow) //判断当前行是否为空行
            {
                this.dataGridView1.Rows.RemoveAt(rowIndex);


            }
         
        }
    }
}

 

转载于:https://www.cnblogs.com/zqyo2000z/p/5401308.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值