PowerShell Studio 2017 5.4.136.0 x64
Windows 2012 R2 (x64)
I am using a DataGridView with an Active Directory Search. The DataGridView SelectionMode is set to FullRowSelect, and only one item can be selected at a time. When selecting a row, the code updates additional text boxes with the information contained in the relevant cell. For example, when selecting a row, the text box labeled HEV Pin: is updated with the value of the EmployeeID in the DataGridView.
When selecting rows, most times the data is updated correctly, however sometimes the event seems to not fire on the row click and the text boxes are left with the data from the previous selection.
I create the DataGridView setting the DataSource to a table created with CovertTo-DataTable.
The CellContentClick Event is as follows:
$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#TODO: Place custom script here
$rowIndex = $datagridview1.CurrentCell.RowIndex
#Write-Debug $rowIndex
$row = $datagridview1.Rows[$rowIndex]
$global:SamAccountName = $row.Cells["SamAccountName"].Value
$global:Name = $row.Cells["Name"].Value
$global:EmployeeID = $row.Cells["EmployeeID"].Value
$global:Role = $row.Cells["Role"].Value
$global:MAC = $row.Cells["MAK"].Value
$global:Soarian = $row.Cells["Soarian"].Value
$global:AccountStatus = $row.Cells["Enabled"].Value
$textboxPin.Text = $global:EmployeeID
$textboxMac.Text = $global:MAC
$textboxSorian.Text = $global:Soarian
if ($global:AccountStatus -eq $false)
{
$checkboxEnableAccount.Enabled = $true
$checkboxEnableAccount.Checked = $true
}
else
{
$checkboxEnableAccount.Enabled = $false
$checkboxEnableAccount.Checked = $false
}
if ($row.Cells["Mailbox"].Value -eq $false)
{
$checkboxEnableEmail.Enabled = $true
}
else
{
$checkboxEnableEmail.Enabled = $false
}
if ($row.Cells["HomeDirectory"].Value -eq $false)
{
$checkboxEnableHomeDirectory.Enabled = $true
}
else
{
$checkboxEnableHomeDirectory.Enabled = $false
}
if ($global:Role -ne "")
{
$combobox1.SelectedIndex = ($combobox1.Items.IndexOf($global:Role))
}
}
I've attached a picture of the incorrect values after a row click that did not update, as well as a short video that shows the issue.
Thank you for your help
DataGridView Event not firing on every click
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.
DataGridView Event not firing on every click
- Attachments
-
- DataGridViewIssue.zip
- Screen Shot and Video
- (178.54 KiB) Downloaded 143 times
Re: DataGridView Event not firing on every click
I recommend using the SelectionChanged event instead of the content click. It will be more reliable.
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: DataGridView Event not firing on every click
Thank you David.
I have been unable to reproduce the issue after making the change in Event Handler.
I have been unable to reproduce the issue after making the change in Event Handler.
- Alexander Riedel
- Posts: 8478
- Last visit: Tue Mar 26, 2024 8:52 am
- Been upvoted: 37 times
Re: DataGridView Event not firing on every click
Please update your account with your subscription. At the moment your account only shows a Virtual Machine Trial from last year.
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
- dan.potter
- Posts: 709
- Last visit: Wed Nov 14, 2018 11:39 am
Re: DataGridView Event not firing on every click
Tip. Instead of the if/else, you can do this.
$checkboxEnableAccount.Enabled = $AccountStatus
$checkboxEnableAccount.Checked = $AccountStatus
$checkboxEnableAccount.Enabled = $AccountStatus
$checkboxEnableAccount.Checked = $AccountStatus
- dan.potter
- Posts: 709
- Last visit: Wed Nov 14, 2018 11:39 am
Re: DataGridView Event not firing on every click
If you attach the psf it will be easier to fix than rebuilding the entire form from scratch.