Support Forums

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.
User avatar
jvierra
Posts: 9693
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: datagridview goes crazy

Postby jvierra » Thu Jan 28, 2016 3:38 pm

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: 88
Joined: Tue Oct 13, 2015 9:10 am

Re: datagridview goes crazy

Postby monoeagle » Thu Jan 28, 2016 3:47 pm

hmmm thanks I understand what the problem is.

$eingelesene_Elemente = $global:EntriesXML.SelectNodes('//Entry')
 
$Typ = ($global:EntriesXML.SelectNodes('//typ')).InnerText
$Baureihe = ($global:EntriesXML.SelectNodes('//baureihe')).InnerText
$Version_Lokal = ($global:EntriesXML.SelectNodes('//version_lokal')).InnerText
$Datum_Lokal = ($global:EntriesXML.SelectNodes('//datum_lokal')).InnerText
$Version_Ablage = ($global:EntriesXML.SelectNodes('//version_ablage')).InnerText
$Datum_Ablage = ($global:EntriesXML.SelectNodes('//datum_ablage')).InnerText
 
$row = @($Typ[$lauf], $Baureihe[$lauf], $Version_Lokal[$lauf], $Datum_Lokal[$lauf], $Version_Ablage[$lauf], $Datum_Ablage[$lauf], $false)
$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
User avatar
jvierra
Posts: 9693
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: datagridview goes crazy

Postby jvierra » Thu Jan 28, 2016 3:55 pm

Fixed!

function Add-DgvEntryFromXml {
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$false)]
[System.String]$XmlFilePath
)
begin {
try
{
#$ConfigXML = New-Object xml
#$ConfigXML.load("c:\temp\Entries.xml")
 
$eingelesene_Elemente = $global:EntriesXML.SelectNodes('//Entry')
$Entry_Counter = $eingelesene_Elemente.Count
$lauf = 0
 
$Typ = ($global:EntriesXML.SelectNodes('//typ')).InnerText
$Baureihe = ($global:EntriesXML.SelectNodes('//baureihe')).InnerText
$Version_Lokal = ($global:EntriesXML.SelectNodes('//version_lokal')).InnerText
$Datum_Lokal = ($global:EntriesXML.SelectNodes('//datum_lokal')).InnerText
$Version_Ablage = ($global:EntriesXML.SelectNodes('//version_ablage')).InnerText
$Datum_Ablage = ($global:EntriesXML.SelectNodes('//datum_ablage')).InnerText
}
catch [exception]{
Write-Output $_.Exception.Message
Exit 1
}
}
process {
try {
foreach ($Entry in $eingelesene_Elemente)
{
if ($Entry_Counter -eq 1) {
$row = @($Typ, $Baureihe, $Version_Lokal, $Datum_Loka, $Version_Ablag, $Datum_Ablage, $false)
$datagridview_main.Rows.Add($row)
$lauf = $lauf + 1
} elseif ($lauf -ne $Entry_Counter) {
$row = @($Typ[$lauf], $Baureihe[$lauf], $Version_Lokal[$lauf], $Datum_Lokal[$lauf], $Version_Ablage[$lauf], $Datum_Ablage[$lauf], $false)
$datagridview_main.Rows.Add($row)
$lauf = $lauf + 1
} else {
#Write-Host "Counter identisch!"
}
}
}
catch [exception]{
Write-Output $_.Exception.Message
Exit 1
}
}
end {
try {
}
catch {
}
}
}


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.
User avatar
jvierra
Posts: 9693
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: datagridview goes crazy

Postby jvierra » Thu Jan 28, 2016 3:56 pm

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.

[xml]$xml=@'
<?xml version="1.0" encoding="utf-8"?>
<inhalt>
<elements>
<Entry Number="1">
<number>1</number>
<typ>VM</typ>
<baureihe>BR402</baureihe>
<version_lokal>V17.22</version_lokal>
<datum_lokal>12.10.2015</datum_lokal>
<version_ablage>V17.22</version_ablage>
<datum_ablage>12.10.2015</datum_ablage>
<pfad_lokal>C:\VMs\VM\BR405_150921_XP</pfad_lokal>
<pfad_ablage>Z:\VMs\VM\BR405_150921_XP</pfad_ablage>
<hash_lokal>
</hash_lokal>
<hash_ablage>
</hash_ablage>
</Entry>
</elements>
</inhalt>
'
@
$xml.inhalt.elements.entry |
select Number,typ,baureihe,version_lokal,datum_lokal,datum_ablage,pfad_lokal,pfad_ablage,hash_lokal,hash_ablage
User avatar
monoeagle
Posts: 88
Joined: Tue Oct 13, 2015 9:10 am

Re: datagridview goes crazy

Postby monoeagle » Thu Jan 28, 2016 4:06 pm

Found it too, couldn't sleep.
However fat thanks for your help.

....
foreach ($Entry in $eingelesene_Elemente)
{
if ($lauf -ne $Entry_Counter)
{
if ($Entry_Counter -eq 1)
{
$row = @($Typ, $Baureihe, $Version_Lokal, $Datum_Lokal, $Version_Ablage, $Datum_Ablage, $false)
}
else
{
$row = @($Typ[$lauf], $Baureihe[$lauf], $Version_Lokal[$lauf], $Datum_Lokal[$lauf], $Version_Ablage[$lauf], $Datum_Ablage[$lauf], $false)
}
$datagridview_main.Rows.Add($row)
$lauf = $lauf + 1
}
....


Your source looks better. I'll read and learn the rest tomorrow, 20h is enough for today.
User avatar
jvierra
Posts: 9693
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: datagridview goes crazy

Postby jvierra » Thu Jan 28, 2016 4:15 pm

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:

$MenuPoint_Add_Click={
[array]$items= $global:EntriesXML.inhalt.elements.entry |
select typ, baureihe, version_lokal, datum_lokal, version_ablage, datum_ablage, Auswahl
$datagridview_main.DataSource=[collections.arraylist]$items
}


That is all it takes to load a grid from XML.
User avatar
monoeagle
Posts: 88
Joined: Tue Oct 13, 2015 9:10 am

Re: datagridview goes crazy

Postby monoeagle » Fri Jan 29, 2016 4:21 am

hi jvierra,

thanks a lot.

greetz
User avatar
jvierra
Posts: 9693
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: datagridview goes crazy

Postby jvierra » Fri Jan 29, 2016 8:24 am

All fixed?

Good luck. That is quite a project.
User avatar
monoeagle
Posts: 88
Joined: Tue Oct 13, 2015 9:10 am

Re: datagridview goes crazy

Postby monoeagle » Fri Jan 29, 2016 10:15 am

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.

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 1 guest