Right click to select a row in a Datagridview and show a menu to delete it

后端 未结 12 536
梦毁少年i
梦毁少年i 2020-12-02 12:00

I have few columns in my DataGridView, and there is data in my rows. I saw few solutions in here, but I can not combine them!

Simply a way to right-click on a row, i

相关标签:
12条回答
  • 2020-12-02 12:24
    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Right)
        {
            MyDataGridView.ClearSelection();
            MyDataGridView.Rows[e.RowIndex].Selected = true;
        }
    }
    
    private void DeleteRow_Click(object sender, EventArgs e)
    {
        Int32 rowToDelete = MyrDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
        MyDataGridView.Rows.RemoveAt(rowToDelete);
        MyDataGridView.ClearSelection();
    }
    
    0 讨论(0)
  • 2020-12-02 12:25

    You can also make this a little simpler by using the following inside the event code:

    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e) 
    {     
        if (e.Button == MouseButtons.Right)     
        {         
            rowToDelete = e.RowIndex;
            MyDataGridView.Rows.RemoveAt(rowToDelete);         
            MyDataGridView.ClearSelection();     
        } 
    }
    
    0 讨论(0)
  • 2020-12-02 12:25

    See here it can be done using the DataGridView RowTemplate property.

    Note: This code isn't tested but I've used this method before.

    // Create DataGridView
    DataGridView gridView = new DataGridView();
    gridView.AutoGenerateColumns = false;
    gridView.Columns.Add("Col", "Col");
    
    // Create ContextMenu and set event
    ContextMenuStrip cMenu = new ContextMenuStrip();
    ToolStripItem mItem = cMenu.Items.Add("Delete");
    mItem.Click += (o, e) => { /* Do Something */ };           
    
    // This makes all rows added to the datagridview use the same context menu
    DataGridViewRow defaultRow = new DataGridViewRow();
    defaultRow.ContextMenuStrip = cMenu;
    

    And there you go, as easy as that!

    0 讨论(0)
  • 2020-12-02 12:28

    It is work for me without any errors:

    this.dataGridView2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown); 
    this.dataGridView2.Click += new System.EventHandler(this.DeleteRow_Click);
    

    And this

    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            var hti = dataGridView2.HitTest(e.X, e.Y);
            dataGridView2.ClearSelection();
            dataGridView2.Rows[hti.RowIndex].Selected = true;
        }
    
    }
    
    
    private void DeleteRow_Click(object sender, EventArgs e)
    {
        Int32 rowToDelete = dataGridView2.Rows.GetFirstRow(DataGridViewElementStates.Selected);
        if (rowToDelete == -1) { }
        else 
        {
            dataGridView2.Rows.RemoveAt(rowToDelete);
            dataGridView2.ClearSelection();
        }
    }
    
    0 讨论(0)
  • 2020-12-02 12:32
    private void dataGridView1_CellContextMenuStripNeeded(object sender, 
    DataGridViewCellContextMenuStripNeededEventArgs e)
    {            
        if (e.RowIndex != -1)
        {
            dataGridView1.ClearSelection();
            this.dataGridView1.Rows[e.RowIndex].Selected = true;
            e.ContextMenuStrip = contextMenuStrip1;
        }
    }
    
    0 讨论(0)
  • 2020-12-02 12:33

    I finally solved it:

    • In Visual Studio, create a ContextMenuStrip with an item called "DeleteRow"

    • Then at the DataGridView link the ContextMenuStrip

    Using the code below helped me getting it work.

    this.MyDataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MyDataGridView_MouseDown);
    this.DeleteRow.Click += new System.EventHandler(this.DeleteRow_Click);
    

    Here is the cool part

    private void MyDataGridView_MouseDown(object sender, MouseEventArgs e)
    {
        if(e.Button == MouseButtons.Right)
        {
            var hti = MyDataGridView.HitTest(e.X, e.Y);
            MyDataGridView.ClearSelection();
            MyDataGridView.Rows[hti.RowIndex].Selected = true;
        }
    }
    
    private void DeleteRow_Click(object sender, EventArgs e)
    {
        Int32 rowToDelete = MyDataGridView.Rows.GetFirstRow(DataGridViewElementStates.Selected);
        MyDataGridView.Rows.RemoveAt(rowToDelete);
        MyDataGridView.ClearSelection();
    }
    
    0 讨论(0)
提交回复
热议问题