Populate Datagridview with Selected record from another Datagridview

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 4 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
ddunst
Posts: 2
Last visit: Mon Nov 16, 2020 7:09 am

Populate Datagridview with Selected record from another Datagridview

Post by ddunst »

I have a main form and a secondary form. In the main form, a datagridview exists where a user can select an individual record.
They right-click and select an option which opens a secondary form.

I have several datagridviews in the secondary form. The goal is to populate one of those datagridviews with the selected record from the Main form's datagridview.
Notes: User is only allowed to select 1 row.
The Datasource for the MainDatagridview is from a SQL Select.

I've attempted passing the SelectedRows[0] as an object to the secondary form
$SelectedRow = $MainDatagridview.SelectedRows[0]
Show-SecondaryForm_psf $SelectedRow
Then setting the datagridview datasource
$SecondaryDataGridview.Datasource = $SelectedRow

Also tried grabbing the index of the selected row, passing that instead
$SelectedRowIndex = $MainDatagridview.SelectedRows[0].Index
$SecondaryDataGridview.Datasource = $MainDataGridView.Datasource[$SelectedRowIndex]

Neither works. But if I set the entire Datasource equal, it populates the secondary datagridview
$SecondaryDataGridview.Datasource = $MainDataGridView.Datasource
If I do this, how can I filter down to just that record.

Am I missing something?
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Populate Datagridview with Selected record from another Datagridview

Post by Alexander Riedel »

[Moved by moderator]
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populate Datagridview with Selected record from another Datagridview

Post by jvierra »

You can copy a row between grids using the row copy method. Between forms sending the row object will work but it likely needs to be cloned.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Populate Datagridview with Selected record from another Datagridview

Post by jvierra »

Here is the CopyTo method of the RowCollection. Send the SelectedRows object and use it with CopyTo to insert the rows in the child form grid.
This topic is 4 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