moving data from one datagridview to 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 7 years and 10 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
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

moving data from one datagridview to another datagridview

Post by sekou2331 »

I am importing a csv and into a gridview with a extra column called selection that has check boxes. I want to be able to click the check box in one grid view and the data be added to another grid view. I trying to understand what I am missing. When I check the bo it gives me numbers in the other gridview. the csv columns are selection, username, computer name, city, IP Address. Added my code below.

  1. function Load-UserInfo (){
  2.    
  3.     $importData = Import-Csv "\\computer\log\Info-20160418 080018.csv" | Select-Object Username, ComputerName, City, "I/P Address" -Unique
  4.    
  5.    
  6.      Load-DataGridView -DataGridView $userDatagridview1  -Item $importData
  7.    
  8.        
  9. }
  10.  
  11. $softwareTreeview1_NodeMouseClick = [System.Windows.Forms.TreeNodeMouseClickEventHandler]{
  12.     if (!(Test-Path -path $_.Node.Name -PathType container))
  13.     {
  14.         return
  15.     }
  16.     $_.Node.Nodes.Clear()
  17.     foreach ($dir in ([System.io.DirectoryInfo]$_.Node.Name).GetFiles())
  18.     {
  19.         $_.Node.Nodes.Add($dir.FullName, $dir.Name)
  20.        
  21.        
  22.     }
  23. }
  24.  
  25. $userDatagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
  26.     #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  27.     #TODO: Place custom script here
  28.    
  29.     $usernameRow = $userDatagridview1.Rows[$_.RowIndex].Cells[1].Value
  30.     $computernameRow = $userDatagridview1.Rows[$_.RowIndex].Cells[2].Value
  31.     $cityRow = $userDatagridview1.Rows[$_.RowIndex].Cells[3].Value
  32.     $IPaddrow = $userDatagridview1.Rows[$_.RowIndex].Cells[4].Value
  33.    
  34.     $usernameRow, $computernameRow, $cityRow, $IPaddrow | ForEach-Object{
  35.        
  36.         Load-DataGridView -DataGridView $installDatagridview2 -Item  $_
  37. }
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: moving data from one datagridview to another datagridview

Post by monoeagle »

At my point I had learned in the last 2 month, Datagridview(DGV) and DataTable(DT) is important.

1. Create DT1.
2. Import the CSV in DT1 and bind it to DGV1.
3. Create DT2 and bind it to DGV2
3. If a CellClick is happen, you get the row counter from the source and copy the row from one DT to the second.

The DGV2 will refresh itself.

The advantage is that you can work in the DT(in memory) and don't have to write directly in the DGV.

regards
mono
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: moving data from one datagridview to another datagridview

Post by jvierra »

You can also just use the "SelectedRows" collection "CopyTo" method to copy the rows directly.

https://msdn.microsoft.com/en-us/librar ... .110).aspx

$dgv1.SelectedRows.CopyTo($dgv2.Rows,0)
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: moving data from one datagridview to another datagridview

Post by sekou2331 »

Hi jvierra,

Your link doesn't work also I was trying to use what you gave me and I am getting the error below
ERROR: Multiple ambiguous overloads found for "CopyTo" and the argument count: "2".
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: moving data from one datagridview to another datagridview

Post by jvierra »

Sorry Not sure what broke the link:
https://msdn.microsoft.com/en-us/librar ... s.90).aspx

This link should work. This forum has a tendency to trash links. I had to be very careful to create it very carefully to get it to work.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: moving data from one datagridview to another datagridview

Post by sekou2331 »

Cool is see the link now. But it seems like it doesn't like the overload for CopyTo. I made my line like below.
  1. $userDatagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
  2.     #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  3.     #TODO: Place custom script here
  4.          $userDatagridview1.SelectedRows.CopyTo($installDatagridview2.Rows, 0)
  5. }
Multiple ambiguous overloads found for "CopyTo" and the argument count: "2".
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: moving data from one datagridview to another datagridview

Post by jvierra »

Unfortunately the "CopyTo" does not appear to work if we are not using a dataset. The recommended method is to manually copy each row and cell.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: moving data from one datagridview to another datagridview

Post by jvierra »

Here is a working example of how to copy rows when there is no dataset.
Attachments
Demo-DGVCopyTo.psf
(23.17 KiB) Downloaded 254 times
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: moving data from one datagridview to another datagridview

Post by sekou2331 »

jvierra,

Thanks for this I see what you did. Got it to populate without the button.
This topic is 7 years and 10 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