Click here to Skip to main content
15,916,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi there,
I have a strange problem with a RightClick Event in my DataGridView.

When you right click on a row in my datagridview, a contextmenustrip is shown where the User can select some items, e.g in one column are folder paths and one item of the contextmenustrip is to open the explorer.exe with this path.

I start the application with 3 rows and I do a rightclick on the second or third row and select to open the Folder, it's the value from the first row (folder path) what is opened in my explorer.exe.
So i have first to left click on a row and afterwards rightclick, to get the correct value from this row.

For the rightClick Event a have the following snippet to select the row and open the contextMenuStrip

C#
if (e.Button == MouseButtons.Right)
{
     //dgv_overAll is the name of my DataGridView
     var hit = dgv_overAll.HitTest(e.X, e.Y);
     dgv_overAll.ClearSelection();
     dgv_overAll.Rows[hit.RowIndex].Selected = true;
     //open now the contextMenuStrip
     cMS_dataGridView.Show();
}



This is the Event from the ContextMenuStrip where i open the explorer:

C#
Process.Start("explorer.exe",dgv_overAll.CurrentRow.Cells[2].Value.ToString();


The row where I perform the rightclick is getting a blue background, so it must be the selected row, isn't is? But why do i get the wrong value? (The value from the last selected row)

I really don't now...

Thanks in advance
Posted

1 solution

Instead of changing the selected for a row in right click event, try changing the current cell to the row that was clicked: DataGridView.CurrentCell Property [^]

Since multiple rows can be selected in a DataGridView the selection doesn't define what row you're on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900