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)
Navigate and get data from datagridview
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.
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.
Re: Navigate and get data from datagridview
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
Re: Navigate and get data from datagridview
Here is a simpler editable DGV.
- Attachments
-
- Demo-DGVDataTableEditExport.psf
- (17.21 KiB) Downloaded 106 times
Re: Navigate and get data from datagridview
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.
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
I've found a way to take the info but don0t know why it only works in PowerShell-ISE and not inside my form.
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?
- $Ttable.Rows |
- ForEach-Object{
- Write-Host ('{0}|{1}|{2}' -f ($_.Cells['Firstname'].Value),($_.Cells['Lastname'].Value), ($_.Cells['Phone Number'].Value))
- }
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
The code to export a DGV is like this:
It requires using a table to build the DGV like this:
The table can also be built with a DataTable object and columns with types if needed.
Code: Select all
$buttonExportCsv_Click = {
$savefiledialog1.AddExtension='csv'
if ('Ok' -eq $savefiledialog1.ShowDialog()) {
$datagridview1.SelectedRows |
Select-Object -expand DataBoundItem |
Export-csv $savefiledialog1.FileName -notype
}
}
Code: Select all
$FormEvent_Load={
$data = '' |select Firstname, Lastname, Age
$datagridview1.DataSource = ConvertTo-DataTable $data
}
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
Here is the full demo as a PS1 file.
- Attachments
-
- Demo-DGVDataTableEditExport.ps1
- (10.08 KiB) Downloaded 101 times