[英]Powershell datagridview cell click event
I have a powershell script in that script i am using datagridview. 我在使用datagridview的脚本中有一个powershell脚本。 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
}
There are plenty of example availble for C# but not found anything for powershell. 对于C#有很多示例可用,但对于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.
我回答的假设是您正在寻找单元格的选择。
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.
对于我的回答,我将坚持您提到的事件: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: 您可以在声明数据网格后添加click事件,如下所示:
$myGrid.Add_CellMouseClick({gridClick})
Note that the name of the function we created is placed within the brackets. 请注意,我们创建的函数的名称位于方括号内。
A few important notes: 一些重要的注意事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.