您的位置:首页 > 编程语言 > C#

C# DataGridvew和contextMenuStrip1控件实现表格删除

2018-03-05 09:18 447 查看


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 DataGrivew.Delete_Row
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        }

        private int RowIndex = 0;
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if(e.Button==MouseButtons.Right)
            {
                this.dataGridView1.Rows[e.RowIndex].Selected = true;//启动点击选中
                RowIndex = e.RowIndex;//读取点击启动行号
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];//设置为单行选中
                this.contextMenuStrip1.Show(this.dataGridView1,e.Location);//设置contextMenuStrip1控件显示和dataGriView控件绑定
                contextMenuStrip1.Show(Cursor.Position);//设置contextMenuStrip1控件的显示座标
            }
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(!this.dataGridView1.Rows[RowIndex].IsNewRow)
            {
                this.dataGridView1.Rows.RemoveAt(RowIndex);//移除行
            }
        }
    }
}

1.添加dataGridView控件和contextMenuStrip控件
2.设置CellMouseDown属性。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐