简体   繁体   English

Powershell DataGridView单元格单击事件

[英]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: 一些重要的注意事项:

  • There are many events to work with. 有许多事件要处理。 While testing this out I tried the selectionChanged, click, cellclick, cellmouseclick, enterrow, and other events. 在测试时,我尝试了selectionChanged,click,cellclick,cellmouseclick,enterrow和其他事件。 You may want to check these out for differences in behavior. 您可能需要检查这些行为的差异。
  • The output may be a little unusual in some cases. 在某些情况下,输出可能会有些异常。 For example the CellMouseClick event seems to respond to clicks in the header. 例如,CellMouseClick事件似乎响应标题中的单击。 So if I click on the cell at row index 1 and column index 1 (second row second column) the script will display the contents appropriately. 因此,如果单击行索引1和列索引1(第二行第二列)的单元格,脚本将适当显示内容。 If I then click on the column heading for the first column, the script will write the same results again (which makes sense because the selected index has not changed) which may seem confusing at first glance. 如果我然后单击第一列的列标题,脚本将再次写入相同的结果(这是有道理的,因为所选索引没有更改),乍一看似乎有些令人困惑。
  • Keep in mind that datagrids allow for multiple selections and I do not know how that might effect the output. 请记住,数据网格允许进行多种选择,但我不知道这会如何影响输出。 That is up to you to figure out. 这取决于您自己确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM