datagridview goes crazy

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 8 years and 1 month 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview goes crazy

Post by jvierra »

The issue seems to occur when there is only one entry in the file. If I add a second entry all records show up correctly but now there a three records instead of two.

From a quick look at your code I am pretty sure it is a design issue.

If you are updating a file from a DGV you need to manage the DGV correctly. When loading a single record beware that you are array subscripting an non-array. Wrap the records in @() before subscripting to avoid subscripting out the first character instead of the field.

Never assume that an operation in PowerShell returns an array. PowerShell preserves singletons for simplicity but it can be a headache to automation.

The fact that you have the first character of every field seems to indicate subscripting into a non-arrayed string.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: datagridview goes crazy

Post by monoeagle »

hmmm thanks I understand what the problem is.
  1. $eingelesene_Elemente = $global:EntriesXML.SelectNodes('//Entry')
  2.            
  3. $Typ            = ($global:EntriesXML.SelectNodes('//typ')).InnerText
  4. $Baureihe       = ($global:EntriesXML.SelectNodes('//baureihe')).InnerText
  5. $Version_Lokal  = ($global:EntriesXML.SelectNodes('//version_lokal')).InnerText
  6. $Datum_Lokal        = ($global:EntriesXML.SelectNodes('//datum_lokal')).InnerText
  7. $Version_Ablage      = ($global:EntriesXML.SelectNodes('//version_ablage')).InnerText
  8. $Datum_Ablage   = ($global:EntriesXML.SelectNodes('//datum_ablage')).InnerText
  9.  
  10. $row = @($Typ[$lauf], $Baureihe[$lauf], $Version_Lokal[$lauf], $Datum_Lokal[$lauf], $Version_Ablage[$lauf], $Datum_Ablage[$lauf], $false)
  11. $datagridview_main.Rows.Add($row)
But @() exist.
Fill every single variable with the equivalent from the xml and then build the $row before adding the row in the dgv.
hmmm

I have shorten the code for reading. Normally the loop the entries from the xml file. The counter is in $lauf.

Thanks so far I have to go to bed. I'll check and test the rest tomorrow.



Edit:

Moment or SelectNodes should be looped SelectSingleNode, hmm I will check tomorrow. gnight
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview goes crazy

Post by jvierra »

Fixed!
  1. function Add-DgvEntryFromXml {
  2.     [CmdletBinding()]
  3.     param(
  4.         [Parameter(Position=0, Mandatory=$false)]
  5.         [System.String]$XmlFilePath
  6.     )
  7.     begin {
  8.         try
  9.         {
  10.             #$ConfigXML = New-Object xml
  11.             #$ConfigXML.load("c:\temp\Entries.xml")
  12.            
  13.             $eingelesene_Elemente = $global:EntriesXML.SelectNodes('//Entry')
  14.             $Entry_Counter = $eingelesene_Elemente.Count
  15.             $lauf = 0
  16.            
  17.             $Typ            = ($global:EntriesXML.SelectNodes('//typ')).InnerText
  18.             $Baureihe       = ($global:EntriesXML.SelectNodes('//baureihe')).InnerText
  19.             $Version_Lokal  = ($global:EntriesXML.SelectNodes('//version_lokal')).InnerText
  20.             $Datum_Lokal    = ($global:EntriesXML.SelectNodes('//datum_lokal')).InnerText
  21.             $Version_Ablage = ($global:EntriesXML.SelectNodes('//version_ablage')).InnerText
  22.             $Datum_Ablage   = ($global:EntriesXML.SelectNodes('//datum_ablage')).InnerText
  23.         }
  24.         catch [exception]{
  25.             Write-Output $_.Exception.Message
  26.             Exit 1
  27.         }
  28.     }
  29.     process {
  30.         try {
  31.             foreach ($Entry in $eingelesene_Elemente)
  32.             {
  33.                 if ($Entry_Counter -eq 1) {
  34.                     $row = @($Typ, $Baureihe, $Version_Lokal, $Datum_Loka, $Version_Ablag, $Datum_Ablage, $false)
  35.                     $datagridview_main.Rows.Add($row)
  36.                     $lauf = $lauf + 1
  37.                 } elseif ($lauf -ne $Entry_Counter) {
  38.                     $row = @($Typ[$lauf], $Baureihe[$lauf], $Version_Lokal[$lauf], $Datum_Lokal[$lauf], $Version_Ablage[$lauf], $Datum_Ablage[$lauf], $false)
  39.                     $datagridview_main.Rows.Add($row)
  40.                     $lauf = $lauf + 1
  41.                 } else {
  42.                     #Write-Host "Counter identisch!"
  43.                 }
  44.             }          
  45.         }
  46.         catch [exception]{
  47.             Write-Output $_.Exception.Message
  48.             Exit 1
  49.         }
  50.     }
  51.     end {
  52.         try {
  53.         }
  54.         catch {
  55.         }
  56.     }
  57. }
Of course it was caused by your unnecessary handling of XML. The XML should not need all of the sub-queries if it is designed correctly.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview goes crazy

Post by jvierra »

Here is how to load the grid without al of the subscripting. Just run the query and add it to the grid. One or 99 records it will always load the same.
  1. [xml]$xml=@'
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <inhalt>
  4.  <elements>
  5.    <Entry Number="1">
  6.      <number>1</number>
  7.      <typ>VM</typ>
  8.      <baureihe>BR402</baureihe>
  9.      <version_lokal>V17.22</version_lokal>
  10.      <datum_lokal>12.10.2015</datum_lokal>
  11.      <version_ablage>V17.22</version_ablage>
  12.      <datum_ablage>12.10.2015</datum_ablage>
  13.      <pfad_lokal>C:\VMs\VM\BR405_150921_XP</pfad_lokal>
  14.      <pfad_ablage>Z:\VMs\VM\BR405_150921_XP</pfad_ablage>
  15.      <hash_lokal>
  16.      </hash_lokal>
  17.      <hash_ablage>
  18.      </hash_ablage>
  19.    </Entry>
  20.  </elements>
  21. </inhalt>
  22. '@
  23. $xml.inhalt.elements.entry |
  24.      select Number,typ,baureihe,version_lokal,datum_lokal,datum_ablage,pfad_lokal,pfad_ablage,hash_lokal,hash_ablage
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: datagridview goes crazy

Post by monoeagle »

Found it too, couldn't sleep.
However fat thanks for your help.
  1. ....
  2.             foreach ($Entry in $eingelesene_Elemente)
  3.             {
  4.                 if ($lauf -ne $Entry_Counter)
  5.                 {
  6.                     if ($Entry_Counter -eq 1)
  7.                     {
  8.                         $row = @($Typ, $Baureihe, $Version_Lokal, $Datum_Lokal, $Version_Ablage, $Datum_Ablage, $false)
  9.                     }
  10.                     else
  11.                     {
  12.                         $row = @($Typ[$lauf], $Baureihe[$lauf], $Version_Lokal[$lauf], $Datum_Lokal[$lauf], $Version_Ablage[$lauf], $Datum_Ablage[$lauf], $false)
  13.                     }
  14.                     $datagridview_main.Rows.Add($row)
  15.                     $lauf = $lauf + 1
  16.                 }
  17. ....
Your source looks better. I'll read and learn the rest tomorrow, 20h is enough for today.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: datagridview goes crazy

Post by jvierra »

To load the grid this is all we do:

1 Give each ciolumn a data name in the designer that matches the XML tags then just do this:
  1. $MenuPoint_Add_Click={
  2.     [array]$items= $global:EntriesXML.inhalt.elements.entry |
  3.              select typ, baureihe, version_lokal, datum_lokal, version_ablage, datum_ablage, Auswahl
  4.     $datagridview_main.DataSource=[collections.arraylist]$items
  5. }
That is all it takes to load a grid from XML.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: datagridview goes crazy

Post by monoeagle »

hi jvierra,

thanks a lot.

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

Re: datagridview goes crazy

Post by jvierra »

All fixed?

Good luck. That is quite a project.
User avatar
monoeagle
Posts: 108
Last visit: Fri Jan 26, 2024 10:44 am

Re: datagridview goes crazy

Post by monoeagle »

At the moment all dgv issues for this tool are fixed. But it can be possible that in the next meeting someone comes with other feature requests. *g*

For now I have added some things to my ToDo List. A lot to learn. =)

Now I have to cleanup and document the Sourcefiles.

Once more big thanks.
This topic is 8 years and 1 month 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