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
datagridview_cellvaluechanged how do i specify which column
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.
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.
- dschmidtberger
- Posts: 37
- Last visit: Fri Oct 01, 2021 6:16 am
- dan.potter
- Posts: 709
- Last visit: Wed Nov 14, 2018 11:39 am
Re: datagridview_cellvaluechanged how do i specify which column
There are a number of ways to do it.
One way.
One way.
- $datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
- $rowindex = $datagridview1.SelectedCells[0].RowIndex
- $datagridview1.Rows[$rowindex].Cells[4].value = 'changed'
- }
- dschmidtberger
- Posts: 37
- Last visit: Fri Oct 01, 2021 6:16 am
Re: datagridview_cellvaluechanged how do i specify which column
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
$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
Re: datagridview_cellvaluechanged how do i specify which column
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.
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:
By usingcolumnnames we eliminate issus caused by possible position changes and programmatic changes.
#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.
- $datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
- $cell= $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex]
- [void][System.Windows.Forms.MessageBox]::Show($cell.Value)
- }
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:
- $datagridview1_CellValueChanged=[System.Windows.Forms.DataGridViewCellEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
- if ($datagridview1.Columns[$_.ColumnIndex].Name -eq 'Network ID') {
- $cell = $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex]
- $datagridview1.Rows[$_.RowIndex].Cells['Fullname'].Value='new value'
- }
- }