Page 3 of 3
Re: datagridview goes crazy
Posted: Thu Jan 28, 2016 2:38 pm
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.
Re: datagridview goes crazy
Posted: Thu Jan 28, 2016 2:47 pm
by monoeagle
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
Re: datagridview goes crazy
Posted: Thu Jan 28, 2016 2:55 pm
by jvierra
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.
Re: datagridview goes crazy
Posted: Thu Jan 28, 2016 2:56 pm
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.
[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
Re: datagridview goes crazy
Posted: Thu Jan 28, 2016 3:06 pm
by monoeagle
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.
Re: datagridview goes crazy
Posted: Thu Jan 28, 2016 3:15 pm
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:
$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.
Re: datagridview goes crazy
Posted: Fri Jan 29, 2016 3:21 am
by monoeagle
hi jvierra,
thanks a lot.
greetz
Re: datagridview goes crazy
Posted: Fri Jan 29, 2016 7:24 am
by jvierra
All fixed?
Good luck. That is quite a project.
Re: datagridview goes crazy
Posted: Fri Jan 29, 2016 9:15 am
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.