Multiple DataGridViews on same form.

Ask your PowerShell-related questions, including questions on cmdlet development!
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 6 years and 7 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
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Multiple DataGridViews on same form.

Post by branhama »

I am working a on a new GUI application which contains a TabControl. On each tab there is a datagrid view which displays data from a SQL query. Now on the first tab the query work fine however on each of the others I get errors like below:

ERROR: Exception calling "Add" with "1" argument(s): "This row already belongs to another table." MainForm.psf (186, 6): ERROR: At Line: 186 char: 6

Now line 186 refers to the ConvertTo-DataTable functions supplied when inserting the first DataGridView. I understand the concept that a new row needs to be created for each line added to a table however these are separate calls to the function in which it creates a new object. Should this not be an entirely new entity?

My call lines are like below:

Code: Select all

$BiosData = ConvertTo-DataTable -InputObject $SQLBiosData -FilterWMIProperties -RetainColumns -Table ONE
Update-DataGridView -DataGridView $datagridviewBios -Item $BiosData -AutoSizeColumns DisplayedCells

$ProcessorData = ConvertTo-DataTable -InputObject $SQLProcessorData -FilterWMIProperties -RetainColumns -Table TWO
Update-DataGridView -DataGridView $datagridviewProcessor -Item $ProcessorData -AutoSizeColumns DisplayedCells
I have tried with the -Table parameter and without however I believe that is a method to break up your input variable if need be. My SQL calls are like below.

Code: Select all

$SQLBiosData = Invoke-Sqlcmd -Query "SELECT Manufacturer,SMBIOSBIOSVersion,SMBIOSMajorVersion,SMBIOSMinorVersion FROM dbo.Bios WHERE PSComputerName = '$($listboxServerList.SelectedItem)'" -ConnectionString $global:SqlConnection -OutputAs DataTables

$SQLProcessorData = Invoke-Sqlcmd -Query "SELECT Manufacturer,MaxClockSpeed,NumberOfCores,NumberOfLogicalProcessors,SocketDesignation,Status FROM dbo.Processor WHERE PSComputerName = '$($listboxServerList.SelectedItem)'" -ConnectionString $global:SqlConnection
Any ideas on how this can be completed?

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

Re: Multiple DataGridViews on same form.

Post by jvierra »

Invoke-SqlCmd returns a datatable so there is no need to use ConvertTo-DataTable.

To display a table in a DGV just assign the table to the DataSource.

Example:

$dt = Invoke-SqlCmd -OutputAs DataTable ...
$datagridview1.DataSource = $dt

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

Re: Multiple DataGridViews on same form.

Post by jvierra »

Here is a demo of the easiest way to build multiple grids on tab pages.
Attachments
Demo=DBTables.psf
(25.66 KiB) Downloaded 198 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple DataGridViews on same form.

Post by jvierra »

Here are some articles on dealing with tables in forms. They may be a bit advanced at this time but are good food for thought on what can be done with data and forms.

http://tech-comments.blogspot.com/2017/03
User avatar
branhama
Posts: 19
Last visit: Mon Jul 03, 2023 12:25 pm

Re: Multiple DataGridViews on same form.

Post by branhama »

jvierra as always you hit it right on and a lot less code than I was using. Thank You!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple DataGridViews on same form.

Post by jvierra »

Not always but my average is pretty good. Glad it made sense. Happy PosHing.
This topic is 6 years and 7 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