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.
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
dlaurora
Posts: 31
Last visit: Wed Oct 02, 2019 12:51 pm

Navigate and get data from datagridview

Post 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)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Navigate and get data from datagridview

Post by jvierra »

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 102 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Navigate and get data from datagridview

Post by jvierra »

Here is a simpler editable DGV.
Attachments
Demo-DGVDataTableEditExport.psf
(17.21 KiB) Downloaded 104 times
User avatar
dlaurora
Posts: 31
Last visit: Wed Oct 02, 2019 12:51 pm

Re: Navigate and get data from datagridview

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Navigate and get data from datagridview

Post by jvierra »

Sorry. You will have to upgrade. A PSF cannot be downgraded.
User avatar
dlaurora
Posts: 31
Last visit: Wed Oct 02, 2019 12:51 pm

Re: Navigate and get data from datagridview

Post 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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Navigate and get data from datagridview

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Navigate and get data from datagridview

Post by jvierra »

Here is the full demo as a PS1 file.
Attachments
Demo-DGVDataTableEditExport.ps1
(10.08 KiB) Downloaded 98 times
This topic is 4 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked