Share via


DataGridView: Get data from rows

Question

Wednesday, April 2, 2014 7:55 AM

Hi, i'm fresching with powershelle and i want to know if there is an option of DataGridView, that allow to get data from rows without select it. i will more clear... i do a grafical inteface where user insert data on many rows. When he finish to insert data  clicking on ok button and all data will be saved in a txt file or csv.

Thanks for the helpfulness

Cristian

All replies (5)

Wednesday, April 2, 2014 5:08 PM âś…Answered

I thought you had a bound data source; that is why my idea didn't work. We can adjust your current script to created a DataTable which can then be bound to the DataGridView and then use that to export the data out to a CSV file when you click the button.

function global:stamp(){
param($Data)
$Data | Export-Csv -NoTypeInformation Test.csv
}

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(800,400)
$go = New-Object System.Windows.Forms.Button
$go.Location = New-Object System.Drawing.Size(300,450)
$go.Size = New-Object System.Drawing.Size(75,23)
$go.text = "ok"
$form.Controls.Add($go)
$form.Controls.Add($dataGridView)
$dataGridView.ColumnHeadersVisible = $true
$ProcessData = New-Object System.Data.DataTable
$ProcessData.tablename = "Process Data"
$Null = $ProcessData.columns.add("Packet")
$Null = $ProcessData.columns.add("Action")
$Null = $ProcessData.columns.add("Dependence_type")
$Null = $ProcessData.columns.add("Pkt_Dependence")
$dataGridView.DataSource = $ProcessData
$go.Add_Click(
{
    Stamp $dataGridView.DataSource 
})

[void]$form.ShowDialog() 

Boe Prox
Blog | Twitter
PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
PowerShell Deep Dives Book


Wednesday, April 2, 2014 10:38 AM | 1 vote

The following should work (you will need to adjust the name based on what you have):

$datagridview.Rows |
select -expand DataBoundItem |
export-csv test.csv -NoType

Boe Prox
Blog | Twitter
PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
PowerShell Deep Dives Book


Wednesday, April 2, 2014 2:28 PM

Sorry for the ignorance, but what do you mean with "adjust the name based oin what yuo have"?

In my DataGridView i insert a string like this:

Thanks so much

Cristian


Wednesday, April 2, 2014 2:37 PM

Sorry for the ignorance, but what do you mean with "adjust the name based oin what yuo have"?

In my DataGridView i insert a string like this:

Thanks so much

Cristian

In your code what do you call your variable that holds the datagridview control? IF your are not sure, please post your code here and we can quickly find it and show the adjustment that needs to happen.

Boe Prox
Blog | Twitter
PoshWSUS | PoshPAIG | PoshChat | PoshEventUI
PowerShell Deep Dives Book


Wednesday, April 2, 2014 3:39 PM

function global:stamp(){
param([string]$string)
$string | Out-File "C:\test.txt" -Append
}

#function global:stamp(){
#$dataGridView.Rows |
#select -expand DataBoundItem |
#export-csv C:\test.csv -NoTypeInformation
#}

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(800,400)
$go = New-Object System.Windows.Forms.Button
$go.Location = New-Object System.Drawing.Size(300,450)
$go.Size = New-Object System.Drawing.Size(75,23)
$go.text = "ok"
$form.Controls.Add($go)
$form.Controls.Add($dataGridView)
$dataGridView.ColumnCount = 4
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns[0].Name = "Packet"
$dataGridView.Columns[1].Name = "Action"
$dataGridView.Columns[2].Name = "Dependence_type"
$dataGridView.Columns[3].Name = "Pkt_Dependence"
$go.Add_Click(
{
    $data=New-Object System.Collections.ArrayList
    for($j=0;$j -le ($dataGridView.RowCount - 2); $j++){
    $dataGridView.Rows[$j].Selected = $true
    for($i=0;$i -le ($dataGridView.Columns.Count -1);$i++){
    $data.Add($dataGridView.Rows[$j].Cells[$i].Value)
    }
    stamp $data 
    $data.clear()   
    }
    
    
    
    
})

[void]$form.ShowDialog() 

This is may code. I find another way to take the values of the rows but i think the other is more faster. Thanks so much for helpfulness

Cristian