Grid: keep focus when refresh(?)

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.
User avatar
stevens
Posts: 434
Joined: Wed Jun 15, 2016 2:26 am

Grid: keep focus when refresh(?)

Post by stevens »

Hi,

I have a gridview which I do a refresh when certain action happens in the gridview.
Now when the refresh happens all items are reloaded and the focus goes to the first row.

Is there a way to keep the focus on the row which was selected before the refresh/reload of the grid?

J

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Grid: keep focus when refresh(?)

Post by jvierra »

The only way I know of is to save the selected row and reselect it after the refresh.

User avatar
stevens
Posts: 434
Joined: Wed Jun 15, 2016 2:26 am

Re: Grid: keep focus when refresh(?)

Post by stevens »

Ok, thanks. And how do I do that?
I can select an item in gridview, but never selected a row, then how to go to that row when refresh happens?

User avatar
stevens
Posts: 434
Joined: Wed Jun 15, 2016 2:26 am

Re: Grid: keep focus when refresh(?)

Post by stevens »

Something like this?
In action selectionchanged
$Global:LastSelectedRowDataGridView = $dataGridView.SelectedRows[0].Index

Then in refresh
$datagridview.SelectedRows = $LastSelectedRowDataGridView

User avatar
stevens
Posts: 434
Joined: Wed Jun 15, 2016 2:26 am

Re: Grid: keep focus when refresh(?)

Post by stevens »

Note: tried solution above but did not work.
It says error "'SelectedRows' is a ReadOnly property..Exception.Message a"

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Grid: keep focus when refresh(?)

Post by jvierra »

You have to get a row object and "select" it.

$dartagridview.Rows[$rowid].Selected = $true

User avatar
stevens
Posts: 434
Joined: Wed Jun 15, 2016 2:26 am

Re: Grid: keep focus when refresh(?)

Post by stevens »

Thanks, that works!
But now the grid has the first row AND the selected item. Is there a property I can deselect so it doesn't select first row in grid too?

Note: when I select another item in the grid, then select first row, after refresh it doesn't select first row but the other row in the grid which is confusing for GUI user.
Last edited by stevens on Fri Oct 30, 2020 2:19 am, edited 1 time in total.

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Grid: keep focus when refresh(?)

Post by jvierra »

Try "$false".

User avatar
stevens
Posts: 434
Joined: Wed Jun 15, 2016 2:26 am

Re: Grid: keep focus when refresh(?)

Post by stevens »

Thanks, that works but then no item is selected in the gridview.

jvierra
Posts: 14611
Joined: Tue May 22, 2007 9:57 am
Answers: 2
Has voted: 1 time
Been upvoted: 3 times
Contact:

Re: Grid: keep focus when refresh(?)

Post by jvierra »

So set the item you want to be selected to $true.

Post Reply