Page 1 of 1

Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 8:28 am
by dlaurora
Hello guys,

I have created a Data Grid view were users will put personal information on it. I'm trying to get that data and save it into a variable but I don't know how to navigate through it, could you please help me with this?

The data grid view contains:

Lastname | Name | Phone Number
Contoso | Jhon | +187655272837
Markson | Steve | +123443243521

$lastname = Column1 (cell1 + cell2)

$name = column2 (cell1 + cell2)

$phone = column3 (cell1 + cell2)

Re: Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 9:38 am
by jvierra
Here is a complete demo of using a DGV to edit and save data to a CSV.

Re: Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 9:45 am
by jvierra
Here is a simpler editable DGV.

Re: Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 10:01 am
by dlaurora
sorry I can't open that file

Unable to load Demo-DGVExport3.psf (File Format: v3.1), because it was created with a different version of SAPIEN PowerShell Studio 2017.

Re: Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 10:21 am
by jvierra
Sorry. You will have to upgrade. A PSF cannot be downgraded.

Re: Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 11:57 am
by dlaurora
I've found a way to take the info but don0t know why it only works in PowerShell-ISE and not inside my form.
  1. $Ttable.Rows |
  2.         ForEach-Object{
  3.             Write-Host ('{0}|{1}|{2}' -f ($_.Cells['Firstname'].Value),($_.Cells['Lastname'].Value), ($_.Cells['Phone Number'].Value))
  4.        }

When running this inside my from, it only retrieves me the last value
is there any other property inside Studio for datagridview that I need to use?

Re: Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 12:29 pm
by jvierra
The code to export a DGV is like this:

Code: Select all

$buttonExportCsv_Click = {
	$savefiledialog1.AddExtension='csv'
	if ('Ok' -eq $savefiledialog1.ShowDialog()) {
		$datagridview1.SelectedRows | 
			Select-Object -expand DataBoundItem |
            Export-csv $savefiledialog1.FileName -notype
	}
}
It requires using a table to build the DGV like this:

Code: Select all

$FormEvent_Load={
	$data = '' |select Firstname, Lastname, Age
    $datagridview1.DataSource = ConvertTo-DataTable $data
}
The table can also be built with a DataTable object and columns with types if needed.

Code: Select all

$dt = [System.Data.DataTable]::New()
[void]$dt.Columns.Add('Firstname', [string])
[void]$dt.Columns.Add('LastName', [string])
[void]$dt.Columns.Add('Age', [int])
$datagridview1.DataSource = $dt

Re: Navigate and get data from datagridview

Posted: Thu Apr 11, 2019 12:34 pm
by jvierra
Here is the full demo as a PS1 file.