Navigate and get data from 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
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Navigate and get data from datagridview

Post by dlaurora » Thu Apr 11, 2019 8:28 am

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)

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

Re: Navigate and get data from datagridview

Post by jvierra » Thu Apr 11, 2019 9:38 am

Here is a complete demo of using a DGV to edit and save data to a CSV.
Attachments
Demo-DGVExport3.psf
(17.26 KiB) Downloaded 32 times

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

Re: Navigate and get data from datagridview

Post by jvierra » Thu Apr 11, 2019 9:45 am

Here is a simpler editable DGV.
Attachments
Demo-DGVDataTableEditExport.psf
(17.21 KiB) Downloaded 27 times

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Navigate and get data from datagridview

Post by dlaurora » Thu Apr 11, 2019 10:01 am

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.

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

Re: Navigate and get data from datagridview

Post by jvierra » Thu Apr 11, 2019 10:21 am

Sorry. You will have to upgrade. A PSF cannot be downgraded.

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Navigate and get data from datagridview

Post by dlaurora » Thu Apr 11, 2019 11:57 am

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.  
  3.         ForEach-Object{
  4.  
  5.             Write-Host ('{0}|{1}|{2}' -f ($_.Cells['Firstname'].Value),($_.Cells['Lastname'].Value), ($_.Cells['Phone Number'].Value))
  6.  
  7.        }

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?

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

Re: Navigate and get data from datagridview

Post by jvierra » Thu Apr 11, 2019 12:29 pm

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

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

Re: Navigate and get data from datagridview

Post by jvierra » Thu Apr 11, 2019 12:34 pm

Here is the full demo as a PS1 file.
Attachments
Demo-DGVDataTableEditExport.ps1
(10.08 KiB) Downloaded 31 times

Locked