Datagridview - Cell Selection and mouse pointer

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.
Isaac99
Posts: 17
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Fri Dec 13, 2019 4:37 am

Datagridview - Cell Selection and mouse pointer

Post by Isaac99 »

Hi
I am capturing the contents of a cell in a datagridview which contains the name of a process:

$datagridviewProcessList_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
$script:proc = $datagridviewProcessList.SelectedCells[0].Value
Write-Host $proc



}
The aim is the user can click on a process name and has the option to kill the process.
The challenge I have is that if the user clicks on the left side of the cell the content is captured to $proc and all works fine.
If however the user inadvertently clicks on the right hand side of the cell, the value isn't captured to $proc.
I can see this by the write-host $proc
In both cases, clicking on the right or left side of the cell, the cell is highlighted as shown in the screenshots, so as far as the user is concerned, they have highlighted the cell.
I've made the cell read only so as there is no option to click into it and rename the cell, which I though might be why it displayed different behavior by clicking on the end of the cell, but no good.
Anyone got any ideas?
click_left.JPG
click_left.JPG (59.88 KiB) Viewed 525 times
click_right.JPG
click_right.JPG (59.9 KiB) Viewed 525 times

jvierra
Posts: 14213
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview - Cell Selection and mouse pointer

Post by jvierra »

Method #1 -Set full row select. When the row is clicked then reference the cell.
Method #2 - Use CellClick event to reference teh cell clicked directly.

$dataGridView1.Rows[$_.RowIndex].Cells[$_.ColumnIndex]

Of course you have to check the cell index to be sure the column chosen is correct.

jvierra
Posts: 14213
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview - Cell Selection and mouse pointer

Post by jvierra »

Also consider using a celldoubleclick which can prevent accidental clicks. WIth doubleclick the cell can be set as read-only and it will still work.

Isaac99
Posts: 17
Joined: Fri Dec 13, 2019 4:37 am

Re: Datagridview - Cell Selection and mouse pointer

Post by Isaac99 »

thanks for the quick reply.
Method #1 - I want users to be able to terminate process by PID also so my users need to be able to click into that cell on same row too so it needs to be cell select
Method #2 - shows the exact same behavior as I had with original method...? i.e. cell is selected on mouse click but only pulls data to variable if left half of cell is clicked?

Isaac99
Posts: 17
Joined: Fri Dec 13, 2019 4:37 am

Re: Datagridview - Cell Selection and mouse pointer

Post by Isaac99 »

thanks for the suggestions.
I have a confirmation form popping up to prevent accidental clicks.
I tried celldouble click just now - it too only works when left hand side of cell is double clicked?!

jvierra
Posts: 14213
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview - Cell Selection and mouse pointer

Post by jvierra »

Works fine for me. I use the CellClick. You are not using the CellCLick event.

jvierra
Posts: 14213
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview - Cell Selection and mouse pointer

Post by jvierra »

"CellDoubleClick" does not do that if the cell is not editable. What is happening is that the cell is entering edit mode on the first click. You cannot have both work the way you want. Make the cell uneditable.

Isaac99
Posts: 17
Joined: Fri Dec 13, 2019 4:37 am

Re: Datagridview - Cell Selection and mouse pointer

Post by Isaac99 »

Thanks. I see from one of your other posts that the cell click event needs to be added in the designer and not just copied and pasted but I can't see how to do this?

jvierra
Posts: 14213
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Datagridview - Cell Selection and mouse pointer

Post by jvierra »

Right click any control and select add events.

Isaac99
Posts: 17
Joined: Fri Dec 13, 2019 4:37 am

Re: Datagridview - Cell Selection and mouse pointer

Post by Isaac99 »

Fantastic, it works a treat now thanks very much

Locked