Datagridview RightClick事件选择不正确的行 [英] Datagridview RightClick Event selects not the correct row

查看:62
本文介绍了Datagridview RightClick事件选择不正确的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我的DataGridView中的RightClick事件有一个奇怪的问题.

在我的datagridview中的一行上单击鼠标右键时,将显示一个contextmenustrip,用户可以在其中选择一些项目,例如,一列是文件夹路径,contextmenustrip的一项是使用此路径打开explorer.exe. >
我以3行启动应用程序,然后在第二行或第三行上单击鼠标右键,然后选择打开文件夹",它是第一行(文件夹路径)中在explorer.exe中打开的值. /> 因此,我首先要在一行上单击鼠标左键,然后再单击鼠标右键,以从该行中获取正确的值.

对于rightClick事件,请使用以下代码段选择该行并打开contextMenuStrip

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

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();
}




这是我打开资源管理器的ContextMenuStrip中的事件:




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

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

推荐答案

而不是在右键单击事件中更改为行选择的内容,请尝试将当前单元格更改为被单击的行:DataGridView.CurrentCell属性 [
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.


这篇关于Datagridview RightClick事件选择不正确的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆