Updating Datagrid

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 2 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
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Updating Datagrid

Post by james.bernard »

Hey all,
been working on adding in a data grid to my form in PowerShell Studio 2020. I have it added to my form, and am trying to update the datagrid every time a certain action is run. It is adding the Custom Objects like I want, but it is not updating them, but rather overwriting whats in there already with the new objects. Can you tell me what I need to change in my code to allow it to add the new objects and not overwrite whats already in there?

Code: Select all

$results = [PSCustomObject] @{
			'Package'  = $Selpackage
			'Computer' = $ComputerNamePDQ
		}
	}
	if ($RunJobs -eq $null)
	{
		$RunJobs = [System.Collections.ArrayList]@()
		$RunJobs += $results
		$PDQgrid.DataSource = [System.Collections.ArrayList]@([pscustomobject]$RunJobs)
	}
	else
	{ $RunJobs += $results
	  $PDQgrid.Rows.Add($RunJobs)
		
	}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating Datagrid

Post by jvierra »

First you are mailing a number of common errors that most people new to programming make. Frist you are trying to state things in negative logical form which can be misleading and cause the logic to be difficult to understand or check. Second you are creati8ng multiple interim variables that are unnecessary and can also easily lead to a difficult to understand failure.

Here is the easiest way to state the issue without inconsistent logic and mucho variables.

Code: Select all

	if(-not $PDQgrid.DataSource){
		$PDQgrid.DataSource = [System.Collections.ArrayList]@()
	}
	$PDQgrid.DataSource += [PSCustomObject]@{
		Package  = $Selpackage
		Computer = $ComputerNamePDQ
       }
Note that only one test is necessary and only one method of adding is required. Also creating the type for the DataSource should be done in the "Load" event and only done once.

The following article can help you to learn how to avoid many of these simple mistakes that most trying to learn programming make .

https://github.com/PoshCode/PowerShellPracticeAndStyle
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Re: Updating Datagrid

Post by james.bernard »

So when I run the code I submitted I get the results in the Data Grid that I was hoping but theyre overwritten. When I run the code you supplied, it updates the Data Grid but has none of the info (see attachments).
Attachments
My original
My original
DataGrid2.png (4.83 KiB) Viewed 2245 times
Code you supplied
Code you supplied
DataGrid.png (1.47 KiB) Viewed 2245 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating Datagrid

Post by jvierra »

There is not enough information to even begin to understand what you are trying to do.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating Datagrid

Post by jvierra »

Part of the issue is that you cannot dynamically add to a bound ArrayList in a DGV. Use a DataTable and it will work correctly.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating Datagrid

Post by jvierra »

Here is a simple example of how to use a DGV too add rows dynamically.
Attachments
Test-DGVTable.psf
(17.12 KiB) Downloaded 74 times
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Re: Updating Datagrid

Post by james.bernard »

Thank you for the reply, but I do not think your link posted.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating Datagrid

Post by jvierra »

Sorry about that.
Attachments
Test-DGVTable.psf
(17.12 KiB) Downloaded 84 times
james.bernard
Posts: 16
Last visit: Wed Oct 26, 2022 8:29 am

Re: Updating Datagrid

Post by james.bernard »

That was exactly what I needed to see, got it working as I needed in my form. Thank you for the help, J.
This topic is 2 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