DataGridView Event not firing on every click

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 3 weeks 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
cahern
Posts: 2
Last visit: Thu Mar 02, 2017 12:42 pm

DataGridView Event not firing on every click

Post by cahern »

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
Attachments
DataGridViewIssue.zip
Screen Shot and Video
(178.54 KiB) Downloaded 143 times
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: DataGridView Event not firing on every click

Post by davidc »

I recommend using the SelectionChanged event instead of the content click. It will be more reliable.
David
SAPIEN Technologies, Inc.
User avatar
cahern
Posts: 2
Last visit: Thu Mar 02, 2017 12:42 pm

Re: DataGridView Event not firing on every click

Post by cahern »

Thank you David.

I have been unable to reproduce the issue after making the change in Event Handler.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: DataGridView Event not firing on every click

Post by Alexander Riedel »

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.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: DataGridView Event not firing on every click

Post by dan.potter »

Tip. Instead of the if/else, you can do this.

$checkboxEnableAccount.Enabled = $AccountStatus
$checkboxEnableAccount.Checked = $AccountStatus
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: DataGridView Event not firing on every click

Post by dan.potter »

If you attach the psf it will be easier to fix than rebuilding the entire form from scratch.
This topic is 7 years and 3 weeks 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