h41msh1c0r

Powershell DataTable DataView

Hi in die Runde,

ich habe ein DataGridView. An dieses habe ich eine DataTable gehängt.
Jetzt möchte ich Spalten Sortieren.
Das Control gibt es her, allerdings sortiert es ja nur die Ansicht, nicht die DataTable.
Nach Googlen hieß das Mittel der Wahl DataView.

Demo im kleinen funktioniert.
$dtEmployee = New-Object system.Data.DataTable "Employee"  
$dcId = New-Object system.Data.DataColumn "Id", ([string])  
$dcName = New-Object system.Data.DataColumn "Name", ([String])  

$dtEmployee.Columns.Add($dcId)
$dtEmployee.Columns.Add($dcName)

$dtEmployee.Clear()

$row1 = ("1", "Test")  
$dtEmployee.Rows.Add($row1)

$row3 = ("7", "ZZZ")  
$dtEmployee.Rows.Add($row3)

$row2 = ("5", "AAAA")  
$dtEmployee.Rows.Add($row2)

$dvEmployee = $dtEmployee.DefaultView

$dvEmployee.Sort = "Id Asc"  
$dtNewEmployee = $dvEmployee.ToTable()

Id Name
-- ----
1  Test
7  ZZZ 
5  AAAA

--- Ergebnis ----
1  Test
5  AAAA
7  ZZZ 

Wenn ich das nun versuche zu verbauen:
$global:DataView = $global:table.DefaultView        
$global:DataView.Sort = "$columnName $direction"  
$global:table = $global:DataView.ToTable()

DataGridViewTextBoxColumn { Name=PxeS, Index=9 }
PxeS
Ausnahme beim Festlegen von "Sort": "Spalte PxeS Ascending wurde nicht gefunden."  
In Zeile:1286 Zeichen:9
+         $global:DataView.Sort = "$columnName $direction"  
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) , SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

Die Spalte heißt wirklich PxeS und die Richtung mit "Ascending" ist auch korrekt.

Ich steh gerade etwas auf dem Schlauch wieso er "PxeS Ascending" komplett als Spalte interpretiert?

VG
Auf Facebook teilen
Auf X (Twitter) teilen
Auf Reddit teilen
Auf Linkedin teilen

Content-ID: 351153

Url: https://administrator.de/forum/powershell-datatable-dataview-351153.html

Ausgedruckt am: 23.07.2025 um 12:07 Uhr

colinardo
Lösung colinardo 09.10.2017 aktualisiert um 16:16:54 Uhr
Servus,
Die Spalte heißt wirklich PxeS und die Richtung mit "Ascending" ist auch korrekt.
Nope: TIPP: "Ascending""ASC" face-wink
DataView.Sort-Eigenschaft
A string that contains the column name followed by "ASC" (ascending) or "DESC" (descending). Columns are sorted ascending by default. Multiple columns can be separated by commas.
Grüße Uwe
H41mSh1C0R
H41mSh1C0R 09.10.2017 um 16:25:53 Uhr
Moin Uwe,

die Richtung hole ich hier:
$direction = [System.ComponentModel.ListSortDirection]::Ascending
        
        if ($column.HeaderCell.SortGlyphDirection -eq 'Descending')  
        {
            $direction = [System.ComponentModel.ListSortDirection]::Descending
        }

Selbst wenn ich dort direkt ASC oder DESC eintrage liefert er einen Fehler.

$directionShort = "ASC"  
$global:DataView.Sort = "$columnName, $directionShort"  

Ergebnis:
Ausnahme beim Festlegen von "Sort": "Spalte ASC wurde nicht gefunden."  
In Zeile:1306 Zeichen:9
+         $global:DataView.Sort = "$columnName, $directionShort"  
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) , SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

VG
colinardo
Lösung colinardo 09.10.2017 aktualisiert um 16:34:02 Uhr
"$columnName, $directionShort"
Das Komma darf dort nicht stehen! Les nochmal den Abschnitt oben ...

Ich schieb mal einen Kaffee latte rüber face-big-smile.

Duck und wech ...
H41mSh1C0R
H41mSh1C0R 09.10.2017 aktualisiert um 16:35:31 Uhr
MAAAAAAN ^^
So ein kleines (dummes) Komma.

*Kaffee Latte Schlürf*

danköö