Powershell datagridview 单元格点击事件 [英] Powershell datagridview cell click event

查看:62
本文介绍了Powershell datagridview 单元格点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 datagridview 的那个脚本中有一个 powershell 脚本.我的脚本工作正常.我觉得一切正常.

I have a powershell script in that script i am using datagridview. My script is working fine. All looks ok to me.

我的问题是我想获取所选行的第一列值.为了实现这一点,我需要添加事件(单元格单击/单元格内容单击/单元格鼠标单击).我无法弄清楚.如何添加事件.

My issue is i want to get the first column value of selected row. To fulfill this i need to add event (cell click/cell content click/cell mouse click). I am not able to figure it out. How to add event.

有人可以就此给我建议吗?

Can somebody please advise me on this?

 $datagridview1_CellMouseClick = [System.Windows.Forms.DataGridViewCellEventHandler]{

write-host $_.RowIndex # This displays the row number selected
write-host $_.ColumnIndex # This displays the column number selected
write-host $datagridview1.Rows[$_.RowIndex].Cells[0].value # This would display the value of the first cell in the selected row
write-host $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].value # This would display the value of the cell selected
}

有很多适用于 C# 的示例,但没有找到适用于 powershell 的任何示例.

There are plenty of example availble for C# but not found anything for powershell.

推荐答案

这根本不是一个愚蠢的问题,但有点令人困惑.您说您想要所选行的第一列值,但是根据您正在查找的输出,您似乎对所选单元格更感兴趣.我的回答是假设单元格的选择就是您要查找的内容.

Not a stupid question at all, but a little confusing. You say you want the first column value of the selected row, however based on the output you are looking for it seems you are more interested in the selected cell. I am answering with the assumption that the selection of the cell is what you are looking for.

这里有许多可能的事件.它们之间的差异很微妙,所以我会让你看看是否有一个事件更适合你的目的.对于我的回答,我将坚持您提到的事件:CellMouseClick.

There are many possible events to work with here. The differences between them are subtle so I will leave it to you to see if there is an event that would work better for your purpose. For my answer I will stick with the event you mentioned: CellMouseClick.

我首先创建要在发生点击时执行的函数.

I start by creating the function that I want to execute when the click occurs.

function gridClick(){
$rowIndex = $myGrid.CurrentRow.Index
$columnIndex = $myGrid.CurrentCell.ColumnIndex
Write-Host $rowIndex
Write-Host $columnIndex 
Write-Host $myGrid.Rows[$rowIndex].Cells[0].value
Write-Host $myGrid.Rows[$rowIndex].Cells[$columnIndex].value}

请注意,您必须决定变量的作用域以及函数与数据网格的交互方式.如果您在脚本级别声明数据网格,那么您应该能够在函数内部按名称引用它.此功能将按照您的问题中列出的确切顺序执行您所说的操作.根据口味编辑.

Please note that you will have to decide how your variables are scoped and how the function will interact with the datagrid. If you are declaring the datagrid at the script level then you should be able to just reference it by name inside the function. This function will do what you said, in the exact order listed in your question. Edit to taste.

您可以在声明数据网格后添加点击事件,如下所示:

You can add the click event, after declaring your datagrid, like this:

$myGrid.Add_CellMouseClick({gridClick})

请注意,我们创建的函数的名称放在括号内.

Note that the name of the function we created is placed within the brackets.

一些重要的注意事项:

  • 有许多事件可以处理.在对此进行测试时,我尝试了 selectionChanged、click、cellclick、cellmouseclick、enterrow 和其他事件.您可能需要检查这些行为的差异.
  • 在某些情况下,输出可能有点异常.例如 CellMouseClick 事件似乎响应标题中的点击.因此,如果我单击行索引 1 和列索引 1(第二行第二列)处的单元格,脚本将适当地显示内容.如果我然后单击第一列的列标题,脚本将再次写入相同的结果(这是有道理的,因为选定的索引没有更改)乍一看似乎令人困惑.
  • 请记住,数据网格允许进行多项选择,我不知道这会如何影响输出.这取决于你自己去弄清楚.

这篇关于Powershell datagridview 单元格点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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