Autor |
Beitrag |
tomo2403
Beiträge: 46
Win10
C#, VS 2019
|
Verfasst: Mi 23.05.18 16:57
Hey Leute,
ich habe in meiner Wpf Anwendung ein DataGrid erstellt und Daten hinzugefügt.
Wenn ich allerdings diese in der Anwendung änder möchte, kommt der Fehler "EditItem ist in dieser Ansicht nicht möglich".
Kann mir jemad sagen was ich da tun soll?
Danke im Voraus!
Moderiert von Th69: Titel geändert (war "DataGird bearbeiten").
Zuletzt bearbeitet von tomo2403 am So 10.06.18 12:29, insgesamt 1-mal bearbeitet
|
|
Th69
Beiträge: 4784
Erhaltene Danke: 1055
Win10
C#, C++ (VS 2017/19/22)
|
Verfasst: Mi 23.05.18 17:47
Was für Daten (d.h. welchen Datentyp) hast du denn hinzugefügt, und wie?
|
|
tomo2403
Beiträge: 46
Win10
C#, VS 2019
|
Verfasst: Mi 23.05.18 18:29
Ich benutze int und strings
Unten ist ein Bild davon.
Einloggen, um Attachments anzusehen!
Zuletzt bearbeitet von tomo2403 am So 10.06.18 12:29, insgesamt 1-mal bearbeitet
|
|
Th69
Beiträge: 4784
Erhaltene Danke: 1055
Win10
C#, C++ (VS 2017/19/22)
|
Verfasst: Mi 23.05.18 18:58
Zeige mal etwas Code, d.h. verwendest du MVVM (ObservableCollection) oder hast du die Daten händisch hinzugefügt?
|
|
tomo2403
Beiträge: 46
Win10
C#, VS 2019
|
Verfasst: Do 24.05.18 14:53
Also, das DataGrid an sich ist leer bis auf die Columns.
Mit einem Button kann man dann belibig viele Items hinzufügen. ID und Punkte werden automatisch erstellt. Denn Spieler Namen kann man selbst eintragen.
Das ist das Button Event:
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
| playername = Convert.ToString(PlayerNameEingabe.Text);
Player player1 = new Player { PlayerName = playername, PlayerID = playerid, PlayerScore = 0, }; DataGird.Items.Add(player1); playerid += 1; |
Das sind die Eigenschaften von den Columns:
C#-Quelltext 1: 2: 3:
| <DataGridTextColumn Binding="{Binding PlayerID}" ClipboardContentBinding="{x:Null}" Header="ID" HeaderStringFormat="PlayerID"/> <DataGridTextColumn Binding="{Binding PlayerName}" ClipboardContentBinding="{x:Null}" Header="Spieler Name"/> <DataGridTextColumn Binding="{Binding PlayerScore}" ClipboardContentBinding="{x:Null}" Header="Punkte"/> |
Zuletzt bearbeitet von tomo2403 am So 10.06.18 12:28, insgesamt 2-mal bearbeitet
|
|
Th69
Beiträge: 4784
Erhaltene Danke: 1055
Win10
C#, C++ (VS 2017/19/22)
|
Verfasst: Do 24.05.18 16:29
Die Fehlermeldung kommt, weil du direkt Items hinzufügst, anstatt die Eigenschaft ItemsSource zu benutzen (und zwar mit einer Klasse welche IList unterstützt, also z.B. List oder ObservableCollection):
C#-Quelltext 1: 2: 3: 4: 5: 6: 7: 8: 9:
| private ObservableCollection<Player> players = new ObservableCollection<Player>();
DataGrid.ItemsSource = players;
Player player = new Player { }; players.Add(player); |
PS: Es heißt DataGrid, nicht DateGird.
PPS: Und wenn du WPF wie WinForms programmierst (d.h. ohne MVVM), dann wird es sehr umständlich und führt zu Fehlern wie diesem.
Für diesen Beitrag haben gedankt: tomo2403
|
|
tomo2403
Beiträge: 46
Win10
C#, VS 2019
|
Verfasst: Do 24.05.18 16:40
Danke jetzt Funktioniert es!
|
|
tomo2403
Beiträge: 46
Win10
C#, VS 2019
|
Verfasst: Sa 09.06.18 12:52
Habe doch nochmal eine Frage:
ich habe jetzt zwar alles genauso gemacht wie geschrieben aber, immer wenn ich die Anwendung starte,
werden einfach die strings und int, in denen die Werte gespeichert sind, nochmal als einzeln in Columns dargestellt. Wie bekomme ich die wieder weg?
Zuletzt bearbeitet von tomo2403 am So 10.06.18 12:36, insgesamt 2-mal bearbeitet
|
|
Th69
Beiträge: 4784
Erhaltene Danke: 1055
Win10
C#, C++ (VS 2017/19/22)
|
Verfasst: Sa 09.06.18 16:06
Hast du evtl. AutoGenerateColumns (noch) aktiviert?
Für diesen Beitrag haben gedankt: tomo2403
|
|
tomo2403
Beiträge: 46
Win10
C#, VS 2019
|
Verfasst: So 10.06.18 12:34
Ja, hatte ich.
Danke!
|
|