clearing data in a datagridview without exiting form

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 5 years and 5 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
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

clearing data in a datagridview without exiting form

Post by jsira2003@yahoo.com »

I want to clear the columns in a datagridview without closing the form. I tried many many ways with no success. This should be easy to do but it wasn't. I do not have a datasource as you will see in the sample code.

$comboboxMotors_SelectedIndexChanged={
#TODO: Place custom script here

[int]$index = $comboboxMotors.SelectedIndex
$textboxFreeSpeed.Text = $motors[$index].FreeSpeed
$textboxStallTorque.Text = $motors[$index].StallTorque
$textboxFreeCurrent.Text = $motors[$index].FreeCurrent
$textboxStallCurrent.Text = $motors[$index].StallCurrent
$textboxNumMotors.Text = 1
$textboxSpecVoltage.Text = $motors[$index].SpecVoltage
$comboboxAltVoltage.Text = $motors[$index].SpecVoltage


[int]$numberOfMotors = 1
[float]$Driving = 1
[float]$Driven = 1
[float]$altVoltage = $motors[$index].SpecVoltage
#take record out of hash table and put into array
[array]$selectedMotor = @()
$selectedMotor = $($motors[$index].MakeModel), $($motors[$index].SpecVoltage), $($motors[$index].FreeSpeed), $($motors[$index].FreeCurrent), $($motors[$index].StallTorque), $($motors[$index].StallCurrent), $($motor[$index].gear1), $($motor[$index].gear2), $($motor[$index].gear3), $($motor[$index].gear4), $($motor[$index].gear5), $($motor[$index].gear6)
#[System.Windows.Forms.MessageBox]::Show("$($motors[$index])", "motors index")
[array]$motorStats = MotorOperation $selectedMotor $numberOfMotors $Driving $Driven $altVoltage

$dataGridView = New-Object System.Windows.Forms.DataGridView

$dataGridView.Size = New-Object System.Drawing.Size(640, 400)

#$dataGridView.AutoSize = $true
$dataGridView.Location = "10,220"
$dataGridView.AutoResizeColumns = $true
$formMotorStats.Controls.remove($dataGridView)
$formMotorStats.Controls.Add($dataGridView)
#Create an unbound DataGridView by declaring a column count.
$dataGridView.ColumnCount = 7
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.ColumnHeadersHeightSizeMode = 'AutoSize'
#Set the column header names.

$dataGridView.Columns[0].DefaultCellStyle.Alignment = 'MiddleCenter'
$dataGridView.Columns[0].Name = "Amps"
$dataGridView.Columns[1].DefaultCellStyle.Alignment = 'MiddleCenter'
$dataGridView.Columns[1].Name = "Torque (N-m)"
$dataGridView.Columns[2].DefaultCellStyle.Alignment = 'MiddleCenter'
$dataGridView.Columns[2].Name = "RPM"
$dataGridView.Columns[3].DefaultCellStyle.Alignment = 'MiddleCenter'
$dataGridView.Columns[3].Name = "Output (W)"
$dataGridView.Columns[4].DefaultCellStyle.Alignment = 'MiddleCenter'
$dataGridView.Columns[4].Name = "Input (W)"
$dataGridView.Columns[5].DefaultCellStyle.Alignment = 'MiddleCenter'
$dataGridView.Columns[5].Name = "Efficiency %"
$dataGridView.Columns[6].DefaultCellStyle.Alignment = 'MiddleCenter'
$dataGridView.Columns[6].Name = "Power %"

#freeze first column
$dataGridView.Columns[0].Frozen = $true


[array]$allRows = @()

foreach ($motorStat in $motorStats)
{
$Rows = New-Object PSObject
$Rows | Add-Member -MemberType NoteProperty -Name Current -Value $([math]::ROUND($motorStat.current, 2))
$Rows | Add-Member -MemberType NoteProperty -Name Torque -Value $([Math]::Round($motorStat.Torque,3))
$Rows | Add-Member -MemberType NoteProperty -Name RPM -Value $([Math]::Round($motorStat.RPM,2))
$Rows | Add-Member -MemberType NoteProperty -Name PowerOutput -Value $([System.Math]::Round($motorStat.PowerOutput,2))
$Rows | Add-Member -MemberType NoteProperty -Name PowerInput -Value $([System.Math]::Round($motorStat.PowerInput,2))
$Rows | Add-Member -MemberType NoteProperty -Name PowerEfficiency -Value $([System.Math]::Round($motorStat.PowerEfficiency,2))
$Rows | Add-Member -MemberType NoteProperty -Name PowerPercentage -Value $([System.Math]::Round($motorStat.PowerPercentage,3))
$allRows += $Rows
}

foreach ($allRow in $AllRows)
{
$dataGridView.Rows.Add($allRow.Current, $allRow.Torque, $allRow.RPM, $allRow.PowerOutput, $allRow.PowerInput, $allRow.PowerEfficiency, $allRow.PowerPercentage)
}

$dataGridView.AutoResizeColumns('AllCells')
$dataGridView.ReadOnly = $true

}

Thanks for your help,
John
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: clearing data in a datagridview without exiting form

Post by davidc »

[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: clearing data in a datagridview without exiting form

Post by jvierra »

To clear rows of a DGV just do this:

$datagridview1.Rows.Clear()

This will remove all data and retain the column definitions. No need to re-create verything.

To add data to a DGV just create a custom "Select-Object" that provides the objects as required and assign to the "DataSource".

$rows = $motorstats | select <computed properties>
$datagridview1.DataSource = [arraylist]$rows

The columns must be defined with a "datamembername" property that matches the objects property names.

You can also just let the DGV auto-create the columns.
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: clearing data in a datagridview without exiting form

Post by jsira2003@yahoo.com »

I tried that line of code many times. Maybe I didn't put it is the right spot. Nevertheless it did not work.

John
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: clearing data in a datagridview without exiting form

Post by jvierra »

Without some idea of how you are doing this I cannot be of much help.

Start by being sure you ae loading the grid with the "DataSource" property. That allows the grid to correctly build the rows.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: clearing data in a datagridview without exiting form

Post by jvierra »

If the rows are locked by the creation method this will work:

$datagridview1.DataSource = $null
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: clearing data in a datagridview without exiting form

Post by jvierra »

The attached sampler demonstrates the many ways to dynamically load and reload a DGV.
Attachments
Delm-DGVClear.psf
(28.84 KiB) Downloaded 131 times
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: clearing data in a datagridview without exiting form

Post by jsira2003@yahoo.com »

I tried both the $dataGridView.DataSource = [System.Collections.Array]$motorStats and this worked. However the $dataGridView.Rows.Clear() and also the $dataGridView.DataSource = $null did not clear the table. I should note I did not use a datagridview object from the powershell studio toolbox. Here is the code presently with the clear and nulls removed:

$comboboxMotors_SelectedIndexChanged={
#TODO: Place custom script here
[int]$index = $comboboxMotors.SelectedIndex
$textboxFreeSpeed.Text = $motors[$index].FreeSpeed
$textboxStallTorque.Text = $motors[$index].StallTorque
$textboxFreeCurrent.Text = $motors[$index].FreeCurrent
$textboxStallCurrent.Text = $motors[$index].StallCurrent
$textboxNumMotors.Text = 1
$textboxSpecVoltage.Text = $motors[$index].SpecVoltage
$comboboxAltVoltage.Text = $motors[$index].SpecVoltage


[int]$numberOfMotors = 1
[float]$Driving = 1
[float]$Driven = 1
[float]$altVoltage = $motors[$index].SpecVoltage
#take record out of hash table and put into array
[array]$selectedMotor = @()
$selectedMotor = $($motors[$index].MakeModel), $($motors[$index].SpecVoltage), $($motors[$index].FreeSpeed), $($motors[$index].FreeCurrent), $($motors[$index].StallTorque), $($motors[$index].StallCurrent), $($motor[$index].gear1), $($motor[$index].gear2), $($motor[$index].gear3), $($motor[$index].gear4), $($motor[$index].gear5), $($motor[$index].gear6)
#[System.Windows.Forms.MessageBox]::Show("$($motors[$index])", "motors index")

[System.Collections.ArrayList]$motorStats = MotorOperation $selectedMotor $numberOfMotors $Driving $Driven $altVoltage

$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size = New-Object System.Drawing.Size(640, 400)

#$dataGridView.AutoSize = $true
$dataGridView.Location = "10,220"
$dataGridView.AutoResizeColumns = $true

$formMotorStats.Controls.Add($dataGridView)

#Create an unbound DataGridView by declaring a column count.

$dataGridView.ColumnHeadersVisible = $true
$dataGridView.ColumnHeadersHeightSizeMode = 'AutoSize'

$dataGridView.DataSource = [System.Collections.ArrayList]$motorStats


$dataGridView.AutoResizeColumns('AllCells')
$dataGridView.ReadOnly = $true

}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: clearing data in a datagridview without exiting form

Post by jvierra »

I suggest that you add the DGV in the designer and set the values. You can then easily manage the DGV using normal methods. There is no gain by recreating the DGV each time.

Also we would add the objects to the ComboBox and just retrieve them from the SelectedItem.

On "index changed" the "SelectedItem" contains the complete object.

What is "MotorOperation"? What does it return?
User avatar
jsira2003@yahoo.com
Posts: 117
Last visit: Tue Jul 11, 2023 6:18 am

Re: clearing data in a datagridview without exiting form

Post by jsira2003@yahoo.com »

MotorOperation returns an entire table to be displayed in the grid. The combobox selection directs which motor operation is to be analyzed and send the appropriate parameters to the function. The function is working correctly and does not need to be changed. It is just that everytime a new motor is selected the table should be cleared and re-created with whatever is returned by the MotorOperation function. The MotorOperation function works correctly. The only thing that is funky is the grid and the ability to clear it on demand. I like the datasource. That works for me. When I tried to create a datasource object with the gui, it would not let me add the data source in the gui. I suspect I would have to define that in my application.

John
This topic is 5 years and 5 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