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.
This topic is 3 years and 4 months 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
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Grid: keep focus when refresh(?)

Post by jvierra »

Try "$false".
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: Grid: keep focus when refresh(?)

Post by stevens »

Thanks, that works but then no item is selected in the gridview.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Grid: keep focus when refresh(?)

Post by jvierra »

So set the item you want to be selected to $true.
This topic is 3 years and 4 months 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