Page 1 of 1

moving data from one datagridview to another datagridview

Posted: Mon May 09, 2016 2:40 pm
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. }

Re: moving data from one datagridview to another datagridview

Posted: Mon May 09, 2016 3:04 pm
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

Re: moving data from one datagridview to another datagridview

Posted: Mon May 09, 2016 3:26 pm
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)

Re: moving data from one datagridview to another datagridview

Posted: Mon May 09, 2016 5:08 pm
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".

Re: moving data from one datagridview to another datagridview

Posted: Mon May 09, 2016 5:31 pm
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.

Re: moving data from one datagridview to another datagridview

Posted: Tue May 10, 2016 4:03 am
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".

Re: moving data from one datagridview to another datagridview

Posted: Tue May 10, 2016 6:55 am
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.

Re: moving data from one datagridview to another datagridview

Posted: Tue May 10, 2016 7:04 am
by jvierra
Here is a working example of how to copy rows when there is no dataset.

Re: moving data from one datagridview to another datagridview

Posted: Tue May 10, 2016 11:25 am
by sekou2331
jvierra,

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