Powershell datagridview cell click event -
i have powershell script in script using datagridview. script working fine. looks ok me.
my issue want first column value of selected row. fulfill need add event (cell click/cell content click/cell mouse click). not able figure out. how add event.
can please advise me on this?
$datagridview1_cellmouseclick = [system.windows.forms.datagridviewcelleventhandler]{ write-host $_.rowindex # displays row number selected write-host $_.columnindex # displays column number selected write-host $datagridview1.rows[$_.rowindex].cells[0].value # display value of first cell in selected row write-host $datagridview1.rows[$_.rowindex].cells[$_.columnindex].value # display value of cell selected }
there plenty of example availble c# not found powershell.
not stupid question @ all, little confusing. want first column value of selected row, based on output looking seems more interested in selected cell. answering assumption selection of cell looking for.
there many possible events work here. differences between them subtle leave see if there event work better purpose. answer stick event mentioned: cellmouseclick.
i start creating function want execute when 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 have decide how variables scoped , how function interact datagrid. if declaring datagrid @ script level should able reference name inside function. function said, in exact order listed in question. edit taste.
you can add click event, after declaring datagrid, this:
$mygrid.add_cellmouseclick({gridclick})
note name of function created placed within brackets.
a few important notes:
- there many events work with. while testing out tried selectionchanged, click, cellclick, cellmouseclick, enterrow, , other events. may want check these out differences in behavior.
- the output may little unusual in cases. example cellmouseclick event seems respond clicks in header. if click on cell @ row index 1 , column index 1 (second row second column) script display contents appropriately. if click on column heading first column, script write same results again (which makes sense because selected index has not changed) may seem confusing @ first glance.
- keep in mind datagrids allow multiple selections , not know how might effect output. figure out.
Comments
Post a Comment