datagridview_cellvaluechanged how do i specify which column

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dschmidtberger
Posts: 37
Last visit: Fri Oct 01, 2021 6:16 am

datagridview_cellvaluechanged how do i specify which column

Post by dschmidtberger »

trying to use a datagridview, when a user enters a network id in a cell, i need to update another cell in the same row with information from the ad account of the user


i see the event for cellvaluechanged, just can't seem to wrap my head around how that works for specific cells as the event seems to be on the entire datagridview
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: datagridview_cellvaluechanged how do i specify which column

Post by dan.potter »

There are a number of ways to do it.

One way.
  1. $datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.    
  4.     $rowindex =  $datagridview1.SelectedCells[0].RowIndex
  5.     $datagridview1.Rows[$rowindex].Cells[4].value = 'changed'
  6.    
  7. }
User avatar
dschmidtberger
Posts: 37
Last visit: Fri Oct 01, 2021 6:16 am

Re: datagridview_cellvaluechanged how do i specify which column

Post by dschmidtberger »

trying your code Dan, it seems to trigger on any cell edit. would prefer to only try to update a value when that specific cell is edited



$termdatagridview_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
$rowindex = $termdatagridview.SelectedCells[0].RowIndex
write-host $termdatagridview.Rows[$rowindex].Cells[2].value

}

column #2 does contain the network id as entered
on intial startup i see this error multiple times:
ERROR: Index operation failed; the array index evaluated to null.
MainForm.psf (2418, 2): ERROR: At Line: 2418 char: 2
ERROR: + write-host $termdatagridview.Rows[$rowindex].Cells[2].value
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidOperation: (:) [], RuntimeException
ERROR: + FullyQualifiedErrorId : NullArrayIndex
ERROR:

once it finishes throwing errors, anytime any cell is changed it outputs the network id value (this is the value i need to pass into get-aduser so i can set the value in cell #9 in the row
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview_cellvaluechanged how do i specify which column

Post by jvierra »

If you notice that the beginning of the event has this line:

#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]

That is tell9ing you that the event comes with an argument which has the event information.

Start by trying to learn how the events work and look up the event to see how it is used.

Here is how we use this event.
  1. $datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.     $cell= $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex]
  4.     [void][System.Windows.Forms.MessageBox]::Show($cell.Value)
  5. }
This gives you the row and cell that has changed. It will file any time any row is changed from th gU or programmatically.

If you try to use "SelectedCells" it can cause errors due to the order of events.

To trap on a specific column we would filter for the column name:
  1. $datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.     if ($datagridview1.Columns[$_.ColumnIndex].Name -eq 'Network ID') {
  4.         $cell = $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex]
  5.         $datagridview1.Rows[$_.RowIndex].Cells['Fullname'].Value='new value'                
  6.     }
  7.  
  8. }
By usingcolumnnames we eliminate issus caused by possible position changes and programmatic changes.
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked