I been stuck trying to add data from a form to fill into the database and then refresh the data grid to display the new added data. As the Projets
entity have other entities IDs as foreign keys to get data, I defined the combo boxes to have two values, a visible and hidden value. When the visible item is selected I want to use the hidden value to populate the Projets
Table after clicking on the add button and display new values in the data grid. I would appreciate any help.
Here are the necessary codes to fix the issue.
The ProjetsView.xaml.cs
:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.Identity.Client.NativeInterop;
using Projet;
using Projet.Controllers;
using Projet.Models;
namespace Projet.Views
{
public partial class Projets : UserControl
{
private readonly ProjetsController _controller;
public Projets()
{
InitializeComponent();
_controller = new ProjetsController();
LoadData();
LoadBDDs();
LoadEnvironnements();
LoadLanguages();
LoadDéveloppeurs();
DevP.SelectionChanged += DevP_SelectionChanged;
BDDP.SelectionChanged += BDDP_SelectionChanged;
EnvP.SelectionChanged += BDDP_SelectionChanged;
LangP.SelectionChanged += BDDP_SelectionChanged;
}
private void LoadData()
{
var projets = _controller.GetAllProjets().
Select(p => new
{
Nom_Projet = p.Nom_Projet,
Description_Projet = p.Description_Projet,
Version_Projet = p.Version_Projet,
Révision_Projet = p.Révision_Projet,
Nom_Développeur = p.Nom_Développeur,
Nom_BDD = p.Nom_BDD,
Nom_Environnement = p.Nom_Environnement,
Nom_Languages = p.Nom_Languages,
Statut_Projet = p.Statut_Projet
}).ToList();
ProjetsData.ItemsSource = projets;
}
private void LoadBDDs()
{
var bdds = _controller.GetBDDs();
BDDP.ItemsSource = bdds;
BDDP.DisplayMemberPath = "Nom_BDD";
BDDP.SelectedValuePath = "ID_BDD"; // Set the hidden value path
}
private void LoadEnvironnements()
{
var environnements = _controller.GetEnvironnements();
EnvP.ItemsSource = environnements;
EnvP.DisplayMemberPath = "Nom_Environnement";
EnvP.SelectedValuePath = "ID_Environnement";
}
private void LoadLanguages()
{
var languages = _controller.GetLanguages();
LangP.ItemsSource = languages;
LangP.DisplayMemberPath = "Nom_Languages";
LangP.SelectedValuePath = "ID_Language";
}
private void LoadDéveloppeurs()
{
var développeurs = _controller.GetDéveloppeurs();
DevP.ItemsSource = développeurs;
DevP.DisplayMemberPath = "Nom_Développeur";
DevP.SelectedValuePath = "ID_Développeur";
}
private void DevP_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (DevP.SelectedItem != null)
{
Développeurs selectedDéveloppeur = (Développeurs)DevP.SelectedItem;
int id = selectedDéveloppeur.ID_Développeur;
string nom = selectedDéveloppeur.Nom_Développeur;
}
}
private void BDDP_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (BDDP.SelectedItem != null)
{
BDDs selectedBDD= (BDDs)BDDP.SelectedItem;
int id = selectedBDD.ID_BDD;
string nom = selectedBDD.Nom_BDD;
}
}
private void EnvP_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (EnvP.SelectedItem != null)
{
Environnements selectedEnv = (Environnements)EnvP.SelectedItem;
int id = selectedEnv.ID_Environnement;
string nom = selectedEnv.Nom_Environnement;
}
}
private void LangP_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (LangP.SelectedItem != null)
{
Languages selectedLang = (Languages)LangP.SelectedItem;
int id = selectedLang.ID_Languages;
string nom = selectedLang.Nom_Languages;
}
}
}
}
The ProjetsView.xaml
:
<UserControl x:Class="Projet.Views.Projets"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:fa="http://schemas.awesome.incremented/wpf/xaml/fontawesome.sharp"
xmlns:local="clr-namespace:Projet.Views"
xmlns:controllers="clr-namespace:Projet.Controllers"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="1000"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<UserControl.Resources>
<controllers:ProjetsController x:Key="ProjetsController" />
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="50"/>
<RowDefinition Height="300"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1000"/>
<!--<ColumnDefinition Width="50"/-->
</Grid.ColumnDefinitions>
<Grid>
<Border BorderBrush="Black" BorderThickness="2" Grid.Column="0" Grid.Row="1">
<StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
<TextBlock Text="Nom Projet :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="69"
FontWeight="Bold" Margin="0,-65,0,0"/>
<TextBox x:Name="NomP" Margin="0,-70,0,0" Height="24" Width="120" BorderBrush="DarkGray"
Background="Transparent" BorderThickness="2" TextWrapping="Wrap"/>
<TextBlock Text="Description :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="69" TextWrapping="Wrap"
FontWeight="Bold" Margin="30,-65,0,0"/>
<TextBox x:Name="DescP" Margin="0,-70,0,0" Height="24" Width="500" BorderBrush="DarkGray"
Background="Transparent" BorderThickness="2" TextWrapping="Wrap"/>
<TextBlock Text="Développeur :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="69" TextWrapping="Wrap"
FontWeight="Bold" Margin="30,-65,0,0"/>
<ComboBox x:Name="DevP" IsEditable="False" Width="100" Height="20" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent"
Foreground="Black" FontSize="10.5" FontWeight="Bold" Margin="0,-65,0,0"
BorderBrush="Black" BorderThickness="2" SelectionChanged="DevP_SelectionChanged"
SelectedValuePath="ID_Développeur" DisplayMemberPath="Nom_Développeur"
ItemsSource="{Binding Source={StaticResource ProjetsController}, Path=GetDéveloppeurs}" />
<TextBlock Text="BDD :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="69" TextWrapping="Wrap"
FontWeight="Bold" Margin="-1880,0,0,0"/>
<ComboBox x:Name="BDDP" IsEditable="False" Width="120" Height="20" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent"
Foreground="Black" FontSize="10.5" FontWeight="Bold" Margin="-1720,0,0,0"
BorderBrush="Black" BorderThickness="2" SelectionChanged="BDDP_SelectionChanged"
SelectedValuePath="ID_BDD" DisplayMemberPath="Nom_BDD"
ItemsSource="{Binding Source={StaticResource ProjetsController}, Path=GetBDDs}" />
<TextBlock Text="Environnements :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="89"
FontWeight="Bold" Margin="-1480,0,0,0"/>
<ComboBox x:Name="EnvP" IsEditable="False" Width="120" Height="20" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent"
Foreground="Black" FontSize="10.5" FontWeight="Bold" Margin="-1260,0,0,0"
BorderBrush="Black" BorderThickness="2" SelectionChanged="EnvP_SelectionChanged" />
<TextBlock Text="Languages :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="89"
FontWeight="Bold" Margin="-1000,0,0,0"/>
<ComboBox x:Name="LangP" IsEditable="False" Width="120" Height="20" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent"
Foreground="Black" FontSize="10.5" FontWeight="Bold" Margin="-830,0,0,0"
BorderBrush="Black" BorderThickness="2" SelectionChanged="LangP_SelectionChanged" />
<TextBlock Text="Status :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="89"
FontWeight="Bold" Margin="-530,0,0,0"/>
<TextBlock Text="Version :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="89"
FontWeight="Bold" Margin="-1880,70,0,0"/>
<TextBox x:Name="VersionP" Margin="-1750,70,0,0" Height="24" Width="120" BorderBrush="DarkGray"
Background="Transparent" BorderThickness="2" TextWrapping="Wrap"/>
<TextBlock Text="Révision :" VerticalAlignment="Center" HorizontalAlignment="Center"
Foreground="White" FontSize="10.5" Height="20" Width="89"
FontWeight="Bold" Margin="-1480,70,0,0"/>
<TextBox x:Name="RévisionP" Margin="-1320,70,0,0" Height="24" Width="120" BorderBrush="DarkGray"
Background="Transparent" BorderThickness="2" TextWrapping="Wrap"/>
<ComboBox x:Name="StatP" IsEditable="False" Width="120" Height="20" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalContentAlignment="Center" Background="Transparent"
Foreground="Black" FontSize="10.5" FontWeight="Bold" Margin="-400,0,0,0"
BorderBrush="Black" BorderThickness="2">
<ComboBoxItem Content="En Dev"/>
<ComboBoxItem Content="En Test"/>
<ComboBoxItem Content="Not Started"/>
<ComboBoxItem Content="Remplacé"/>
<ComboBoxItem Content="Validé"/>
</ComboBox>
<Button Background="Blue" x:Name="AddBtn" Click="AddBtn_Click" HorizontalAlignment="Center"
Cursor="Hand" Margin="-100,30,0,0"
Height="40" Width="40">
<fa:IconImage Margin="0,0,0,0" Icon="Add" Foreground="Black"
Width="16" Height="50" IconFont="Solid" />
</Button>
</StackPanel>
</Border>
</Grid>
<DataGrid BorderBrush="Black" BorderThickness="2" x:Name="ProjetsData" Background="Transparent" Grid.Row="2" Grid.Column="0"
AutoGenerateColumns="True" ColumnWidth="115" RowHeight="30"
HorizontalScrollBarVisibility="Hidden" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="2" Grid.Column="0"
Margin="900,30,0,250">
<Button Background="Green" x:Name="EditBtn" HorizontalAlignment="Center" Cursor="Hand"
Margin="0,0,0,0"
Height="30" Width="25" >
<fa:IconImage Margin="0,0,0,9" Icon="Edit" Foreground="Black"
Width="17" Height="25" IconFont="Solid" Grid.Row="2"
/>
</Button>
<Button x:Name="DelBtn" Background="Red" HorizontalAlignment="Center"
VerticalAlignment="Center" Cursor="Hand" Margin="0,0,0,0"
Height="30" Width="25" >
<fa:IconImage Margin="0,0,0,0" Icon="X" Foreground="Black"
Width="10" Height="25" IconFont="Solid" Grid.Column="0" Grid.Row="2"
/>
</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0">
<TextBlock Background="Black" VerticalAlignment="Center"
Margin="200,0,0,0"
Foreground="White" Height="30">
<fa:IconImage Icon="Search" Foreground="White"
Width="22" Height="50" IconFont="Solid"
/>
</TextBlock>
<TextBox TextAlignment="Left" x:Name="searchBox" Height="30" Width="400" Margin="0,0,10,0"/>
<Button x:Name="searchButton" Margin="0,0,0,0" Height="30"
Content="Search" Width="80" />
</StackPanel>
</Grid>
</UserControl>
The entities Projets
and BDDs
(other entities are defined the same way as BDDs)
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.Windows.Controls;
using Projet.Views;
namespace Projet.Models
{
public class Projets
{
[Key]
public int ID_Projet { get; set; }
[Required(ErrorMessage = "Le nom de de projet est obligatoire")]
public string Nom_Projet { get; set; }
public string Description_Projet { get; set; }
[Required]
public string Version_Projet { get; set; }
[Required]
public string Révision_Projet { get; set; }
[Required(ErrorMessage = "La version de projet est obligatoire")]
public string Statut_Projet { get; set; }
// Navigation property for one BDD (many Projets belong to one BDD)
public int ID_BDD { get; set; }
public BDDs BDD { get; set; }
public string Nom_BDD { get; set; } // Name property of BDD
// Navigation property for one Environnement (many Projets belong to one Environnement)
public int ID_Environnement { get; set; }
public Environnements Environnement { get; set; }
public string Nom_Environnement { get; set; } // Name property of Environnement
// Navigation property for one Développeur (many Projets belong to one Développeur)
public int ID_Développeur { get; set; }
public Développeurs Développeur { get; set; }
public string Nom_Développeur { get; set; } // Name property of Développeur
// Navigation property for one Language (many Projets belong to one Language)
public int ID_Languages { get; set; }
public Languages Language { get; set; }
public string Nom_Languages { get; set; } // Name property of Languages
// Collection navigation property for many ProjetsToolbox (one Projet can have many ProjetsToolbox)
public ICollection<ProjetsToolbox> ProjetToolbox { get; set; }
// Collection navigation property for many ProjetsSites (one Projet can have many ProjetsSites)
public ICollection<ProjetsSites> ProjetSite { get; set; }
// Navigation property for one Planning (one Projet has one Planning)
public Plannings Planning { get; set; }
}
}
And
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
namespace Projet.Models
{
public class BDDs
{
[Key]
public int ID_BDD { get; set; }
public string Nom_BDD { get; set; }
//
public ICollection<Projets> Projet { get; set; }
}
}
and the ProjetsController.cs
:
using Projet.Models;
using System.Linq;
using System.Collections.Generic;
using Projet;
using Microsoft.EntityFrameworkCore;
namespace Projet.Controllers
{
public class ProjetsController
{
private readonly ProjetContext _context;
public ProjetsController()
{
_context = new ProjetContext();
}
public List<Projets> GetAllProjets()
{
return _context.Projets
.Include(p => p.Développeur)
.Include(p => p.BDD)
.Include(p => p.Environnement)
.Include(p => p.Language)
.Select(p => new Projets
{
Nom_Projet = p.Nom_Projet,
Description_Projet = p.Description_Projet,
Version_Projet = p.Version_Projet,
Révision_Projet = p.Révision_Projet,
Nom_Développeur = p.Développeur != null ? p.Développeur.Nom_Développeur : "",
Nom_BDD = p.BDD != null ? p.BDD.Nom_BDD : "",
Nom_Environnement = p.Environnement != null ? p.Environnement.Nom_Environnement : "",
Nom_Languages = p.Language != null ? p.Language.Nom_Languages : "",
Statut_Projet = p.Statut_Projet
})
.ToList();
}
public List<BDDs> GetBDDs()
{
return _context.BDDs.ToList();
}
public List<Environnements> GetEnvironnements()
{
return _context.Environnements.ToList();
}
public List<Développeurs> GetDéveloppeurs()
{
return _context.Développeurs.ToList();
}
public List<Languages> GetLanguages()
{
return _context.Languages.ToList();
}
}
}