Setting up my Projets.cs
table for binding with a data grid. I got that error Unable to cast object of type 'System.Int32' to type 'System.String'.
. I Tried adding converters to the code. The primary key and foreign keys defined in the model are the issue. Thing is I don’t want them to be visible in the data grid but instead I get the attributes defined in those models each by its foreign key defined in this the Projet table. Is this possible ? I been stuck for days because I am forced to use MVC instead of MVVM in a desktop app. So i would appreciate any help as this is my last resort.
Here is my Projets.cs
Code :
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; }
//////
public int ID_BDD { get; set; }
public BDDs BDD { get; set; }
//////
public int ID_Environnement { get; set; }
public Environnements Environnement { get; set; }
//////
public int ID_Développeur { get; set; }
public Développeurs Développeur { get; set; }
//////
public int ID_Languages { get; set; }
public Languages Language { get; set; }
//////
public ICollection<ProjetsToolbox> ProjetToolbox { get; set; }
//////
public ICollection<ProjetsSites> ProjetSite { get; set; }
//////
public Plannings Planning { get; set; }
}
}
My ProjetsController.cs
using Projet.Models;
using System.Linq;
using System.Collections.Generic;
namespace Projet.Controllers
{
public class ProjetsController
{
private readonly ProjetContext _context;
public ProjetsController()
{
_context = new ProjetContext();
}
public List<Projets> GetAllProjets()
{
return _context.Projets.ToList();
}
}
The Projets.xaml.cs
:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
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();
}
private void LoadData()
{
try
{
List<Projet.Models.Projets> projets = _controller.GetAllProjets();
foreach (var projet in projets)
{
// Assuming 'SomeIntegerProperty' is an integer property in your 'Projets' model
string stringValue = projet.ID_Projet.ToString();
string stringValue1 = projet.ID_BDD.ToString();
string stringValue2= projet.ID_Développeur.ToString();
string stringValue3 = projet.ID_Environnement.ToString();
string stringValue4 = projet.ID_Languages.ToString();
// Now you can use 'stringValue' as needed
}
ProjetsData.ItemsSource = projets;
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred while loading data: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
The Projets.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="850"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
<UserControl.Resources>
<controllers:ProjetsController x:Key="ProjetsController" />
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="400"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="750"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<DataGrid x:Name="ProjetsData" Background="Transparent" Grid.Row="1" Grid.Column="0"
AutoGenerateColumns="False"
ItemsSource="{Binding Projets, Source={StaticResource ProjetsController}}">
</DataGrid>