问题
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
}
There are plenty of example availble for C# but not found anything for powershell.
回答1:
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.
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.
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. 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. 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. 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.
来源:https://stackoverflow.com/questions/31284508/powershell-datagridview-cell-click-event