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.

Code: Select all

function Load-UserInfo (){

$importData = Import-Csv "\\computer\log\Info-20160418 080018.csv" | Select-Object Username, ComputerName, City, "I/P Address" -Unique


Load-DataGridView -DataGridView $userDatagridview1 -Item $importData


}

$softwareTreeview1_NodeMouseClick = [System.Windows.Forms.TreeNodeMouseClickEventHandler]{
if (!(Test-Path -path $_.Node.Name -PathType container))
{
return
}
$_.Node.Nodes.Clear()
foreach ($dir in ([System.io.DirectoryInfo]$_.Node.Name).GetFiles())
{
$_.Node.Nodes.Add($dir.FullName, $dir.Name)


}
}

$userDatagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#TODO: Place custom script here

$usernameRow = $userDatagridview1.Rows[$_.RowIndex].Cells[1].Value
$computernameRow = $userDatagridview1.Rows[$_.RowIndex].Cells[2].Value
$cityRow = $userDatagridview1.Rows[$_.RowIndex].Cells[3].Value
$IPaddrow = $userDatagridview1.Rows[$_.RowIndex].Cells[4].Value

$usernameRow, $computernameRow, $cityRow, $IPaddrow | ForEach-Object{

Load-DataGridView -DataGridView $installDatagridview2 -Item $_
}

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.

Code: Select all

$userDatagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#TODO: Place custom script here
$userDatagridview1.SelectedRows.CopyTo($installDatagridview2.Rows, 0)
}
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.