2 pièce(s) jointe(s)
Ajout/Supp/Modif DataGrid sans formulaire
Bonjour à tous,
Je récupère des données de fichiers .xml afin d'en faire mon ItemSource.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #region Constructor
public OperateurDataGridViewModel()
{
DataGridSource = new ObservableCollection<object>();
foreach (var res in OperateurProvider.getFolderDataPath())
{
DataGridSource.Add(new Operateur
{
ID = res.Attribute("ID").Value,
Prenom = res.Element("Prenom").Value,
Nom = res.Element("Nom").Value,
Equipe = res.Element("Equipe").Value,
Ligne = res.Element("Ligne").Value,
Gap = res.Element("Gap").Value,
GapLeader = res.Element("GapLeader").Value,
SST = res.Element("SST").Value,
});
}
}
#endregion Constructor |
Mon modèle associé est celui-ci
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| #region Properties
//Implémenter l'interface INotify sur chaque prop
public string ID { get; set; }
private string _prenom;
public string Prenom
{
get
{
Console.WriteLine($"[test] {_prenom}");
return _prenom;
}
set
{
_prenom = value;
OnPropertyChanged("Prenom");
}
}
public string Nom { get; set; }
public string Equipe { get; set; }
public string Ligne { get; set; }
public string Gap { get; set; }
public string GapLeader { get; set; }
public string SST { get; set; }
#endregion Properties |
Je bind sans problème le tout dans ma vue
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <DataGrid Grid.Row="1" Grid.Column="1" AutoGenerateColumns="False" AlternatingRowBackground="LightGray" ItemsSource="{Binding DataGridSource, UpdateSourceTrigger=LostFocus}">
<DataGrid.Columns>
<DataGridTextColumn Width="*" Header="Prenom" Binding="{Binding Prenom}" />
<DataGridTextColumn Width="*" Header="Nom" Binding="{Binding Nom}" />
<DataGridTextColumn Width="*" Header="Equipe" Binding="{Binding Equipe}" />
<DataGridTextColumn Width="*" Header="Ligne" Binding="{Binding Ligne}" />
<DataGridTextColumn Width="*" Header="Gap" Binding="{Binding Gap}" />
<DataGridTextColumn Width="*" Header="Gap Leader" Binding="{Binding GapLeader}" />
<DataGridTextColumn Width="*" Header="SST" Binding="{Binding SST}" />
</DataGrid.Columns>
</DataGrid> |
Pièce jointe 554820
Je souhaite pouvoir créer, modifier, supprimer directement des données à partir du dataGrid.
En parcourant le forum, je suis tombé sur ce poste https://www.developpez.net/forums/d9...-datagridview/
Tomlev disait (ca doit toujours être d'actualité)
Citation:
Pour une liste d'objets c'est un peu plus complexe :
- les objets de la liste doivent implémenter INotifyPropertyChanged pour déclencher un évènement quand une valeur de propriété change (sinon le DataGridView ne pourra pas le savoir)
- la liste elle-même doit implémenter IBindingList, pour déclencher un évènement quand la liste change (ajout ou suppression d'éléments). Le plus simple est d'utiliser la classe BindingList<T> qui gère déjà tout ça
J'ai suivi les conseils (en implémentant l'interface INotify), mais lors de la saisie d'une ligne, je récupère 4 fois la valeur insérée...
En implémentant IBindingList et la fonction Add c'est la même chose.
Pièce jointe 554823
Sauriez-vous ce qui cloque ?
PS : le code est un peu cracra, il sera épuré par la suite :aie: