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.
Locked
User avatar
sekou2331
Posts: 267
Joined: Wed Aug 15, 2012 6:07 am

moving data from one datagridview to another datagridview

Post by sekou2331 » Mon May 09, 2016 2:40 pm

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.    
  4.  
  5.     $importData = Import-Csv "\\computer\log\Info-20160418 080018.csv" | Select-Object Username, ComputerName, City, "I/P Address" -Unique
  6.  
  7.    
  8.  
  9.    
  10.  
  11.      Load-DataGridView -DataGridView $userDatagridview1  -Item $importData
  12.  
  13.    
  14.  
  15.        
  16.  
  17. }
  18.  
  19.  
  20.  
  21. $softwareTreeview1_NodeMouseClick = [System.Windows.Forms.TreeNodeMouseClickEventHandler]{
  22.  
  23.     if (!(Test-Path -path $_.Node.Name -PathType container))
  24.  
  25.     {
  26.  
  27.         return
  28.  
  29.     }
  30.  
  31.     $_.Node.Nodes.Clear()
  32.  
  33.     foreach ($dir in ([System.io.DirectoryInfo]$_.Node.Name).GetFiles())
  34.  
  35.     {
  36.  
  37.         $_.Node.Nodes.Add($dir.FullName, $dir.Name)
  38.  
  39.        
  40.  
  41.        
  42.  
  43.     }
  44.  
  45. }
  46.  
  47.  
  48.  
  49. $userDatagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
  50.  
  51.     #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  52.  
  53.     #TODO: Place custom script here
  54.  
  55.    
  56.  
  57.     $usernameRow = $userDatagridview1.Rows[$_.RowIndex].Cells[1].Value
  58.  
  59.     $computernameRow = $userDatagridview1.Rows[$_.RowIndex].Cells[2].Value
  60.  
  61.     $cityRow = $userDatagridview1.Rows[$_.RowIndex].Cells[3].Value
  62.  
  63.     $IPaddrow = $userDatagridview1.Rows[$_.RowIndex].Cells[4].Value
  64.  
  65.    
  66.  
  67.     $usernameRow, $computernameRow, $cityRow, $IPaddrow | ForEach-Object{
  68.  
  69.        
  70.  
  71.         Load-DataGridView -DataGridView $installDatagridview2 -Item  $_
  72.  
  73. }

User avatar
monoeagle
Posts: 107
Joined: Tue Oct 13, 2015 9:10 am

Re: moving data from one datagridview to another datagridview

Post by monoeagle » Mon May 09, 2016 3:04 pm

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: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: moving data from one datagridview to another datagridview

Post by jvierra » Mon May 09, 2016 3:26 pm

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: 267
Joined: Wed Aug 15, 2012 6:07 am

Re: moving data from one datagridview to another datagridview

Post by sekou2331 » Mon May 09, 2016 5:08 pm

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: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: moving data from one datagridview to another datagridview

Post by jvierra » Mon May 09, 2016 5:31 pm

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: 267
Joined: Wed Aug 15, 2012 6:07 am

Re: moving data from one datagridview to another datagridview

Post by sekou2331 » Tue May 10, 2016 4:03 am

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.  
  3.     #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  4.  
  5.     #TODO: Place custom script here
  6.  
  7.          $userDatagridview1.SelectedRows.CopyTo($installDatagridview2.Rows, 0)
  8.  
  9. }
Multiple ambiguous overloads found for "CopyTo" and the argument count: "2".

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

Re: moving data from one datagridview to another datagridview

Post by jvierra » Tue May 10, 2016 6:55 am

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: 13990
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: moving data from one datagridview to another datagridview

Post by jvierra » Tue May 10, 2016 7:04 am

Here is a working example of how to copy rows when there is no dataset.
Attachments
Demo-DGVCopyTo.psf
(23.17 KiB) Downloaded 108 times

User avatar
sekou2331
Posts: 267
Joined: Wed Aug 15, 2012 6:07 am

Re: moving data from one datagridview to another datagridview

Post by sekou2331 » Tue May 10, 2016 11:25 am

jvierra,

Thanks for this I see what you did. Got it to populate without the button.

Locked