0

I have created a code that will do all this step:
1. right click on any index in datagridview.
2. select delete option in toolstripmenu
3. the current row selected highlighted
4. confirm delete.
5. do delete.

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
    {
        var confirmDelete = MessageBox.Show("Are you sure to delete current selected row?", "Row Deleted", MessageBoxButtons.YesNo);
        if (confirmDelete == DialogResult.Yes)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
            }
        }
    }

This code working just the step 3 did not happen. I want it to be like this.

enter image description here

Ren
  • 765
  • 4
  • 15
  • 42

1 Answers1

0

Write an event handler for CellMouseClick event of the datagridview

private void DataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e) 
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Button == MouseButtons.Right)
    {
       dgvBookmarks.Rows[e.RowIndex].Selected = true;
       Rectangle r = dgvBookmarks.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);

       contextMenu.Show((Control)sender, r.Left + e.X, r.Top + e.Y);
    }
}
Suresh Kumar Veluswamy
  • 4,193
  • 2
  • 20
  • 35
  • If this does not work, can you tr using HitTest by handling MouseDown event... private void DataGridView1_MouseDown(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Right) { var hti = dataGridView1.HitTest(e.X, e.Y); dataGridView1.ClearSelection(); dataGridView1Rows[hti.RowIndex].Selected = true; } } Hope this one helps!!! – Suresh Kumar Veluswamy Mar 12 '14 at 09:40