Reputation: 91
I want to make it so that if the first row is right clicked and an option on the context menu is clicked it will do a specific function, and if the second row is right clicked it will do a specific function etc. So I've tried several different codes and none work but this is just my simplified version of the code, so how can I make it do as I want?
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
DataGridViewRow row = new DataGridViewRow();
if (row.Selected.Equals(0) == true && e.Button == MouseButtons.Right && contextMenuStrip1.Text == "Test")
{
MessageBoxEx.Show("Test ok");
}
}
Upvotes: 0
Views: 137
Reputation: 2587
Your purpose is perform different task for different gridview row with same menu item click event.
1- On mouse down, just save the DataGridView rowIndex.
2- On menu item click event, use the saved rowindex to decide your different task.
3- Since Mouse click will fire after context menu hence use MouseDown Instead mouse click event.
int RowIndex = 0;
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (dataGridView1.CurrentRow == null)
return;
if (e.Button == MouseButtons.Right)
{
RowIndex = dataGridView1.CurrentRow.Index ;
}
}
private void testToolStripMenuItem_Click(object sender, EventArgs e) //MenuStrip item click event
{
if (RowIndex == 0)
{
}
else if (RowIndex == 1)
{
}
}
Upvotes: 1