Hi,@Mesh Ka.Welcome Microsoft Q&A.
1.For sorting, filtering and grouping data in DataGrid, you could try to refer to How to: Group, sort, and filter data in the DataGrid control.
2.For the problem of clicking to expand row details, you can try to use something about DataGrid.RowDetailsTemplate You can also refer to my solution in the thread Convert DataGrid.ItemsSource to DataTable.
<DataGrid Name="dg" Width="300" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Details}" Margin="10" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
3.For the problem of expanding or shrinking the RowDetails of the DataGrid through Button, you can set the DataGrid.RowDetailsVisibilityMode binding property that change by button. For more details, see How to: Display and Configure Row Details in the DataGrid Control
By the way, it is recommended that you only post one question per thread. It's best to ask specific questions.
Update:
Right-click Dependencies under the project name and select Manage Nuget Packages... and Search Microsoft.EntityFrameworkCore.SqlServer(7.0.11)
sql:
CREATE TABLE [dbo].[EmployeesContacts] (
[TelephoneID] INT IDENTITY (1, 1) NOT NULL,
[EmployeeID] INT NOT NULL,
[Telephone] NVARCHAR (12) NOT NULL,
CONSTRAINT [PK_EmployeesContacts] PRIMARY KEY CLUSTERED ([TelephoneID] ASC),
CONSTRAINT [FK_EmployeesContacts_Employees] FOREIGN KEY ([EmployeeID]) REFERENCES [dbo].[Employees] ([EmployeeID])
);
xaml:
<Window.Resources>
<CollectionViewSource x:Key="CvsKey">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Employee"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" x:Name="dataGrid1" ItemsSource="{Binding Employees}" IsReadOnly="True" RowDetailsVisibilityMode="Collapsed"
CanUserAddRows="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding EmployeeID}" Header="EmployeeID"/>
<DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"/>
<DataGridTextColumn Binding="{Binding LastName}" Header="LastName"/>
<DataGridTextColumn Binding="{Binding Age}" Header="Age"/>
<DataGridTextColumn Binding="{Binding Gender}" Header="Gender"/>
<DataGridTextColumn Binding="{Binding Email}" Header="Email"/>
<DataGridTemplateColumn Header="Contects" Width="75">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Contacts}" IsReadOnly="True" AutoGenerateColumns="False" HeadersVisibility="None">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Telephone}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Duties" Width="75">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Duties}" IsReadOnly="True" AutoGenerateColumns="False" HeadersVisibility="None">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Duty}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Languages" Width="75">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Languages}" IsReadOnly="True" AutoGenerateColumns="False" HeadersVisibility="None">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding LanguageName}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
Codebedhind:
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace WpfApp1
{
public partial class MainWindow : Window
{
private EmployeeViewModel _viewModel;
public MainWindow()
{
InitializeComponent();
_viewModel = new EmployeeViewModel();
DataContext = _viewModel;
}
}
public class EmployeeViewModel
{
private readonly EmployeeDbContext _context;
public EmployeeViewModel()
{
using (var context = new EmployeeDbContext())
{
Employees = context.Employees
.Include(e => e.Duties)
.Include(e => e.Contacts)
.Include(e => e.Languages)
.ToList();
}
}
public List<Employee> Employees { get; set; } = new List<Employee>();
public void SaveChanges()
{
_context.SaveChanges();
}
// Add methods for CRUD operations as needed
}
public class EmployeeDbContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<EmployeesContact> EmployeesContacts { get; set; }
public DbSet<EmployeesDuty> EmployeesDuties { get; set; }
public DbSet<EmployeesLanguage> EmployeesLanguages { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Configure your database connection here
optionsBuilder.UseSqlServer("Data Source=(localdb)\\ProjectModels;Initial Catalog=EmployeesDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False");
}
}
public class Employee
{
public int EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public string Email { get; set; }
public ObservableCollection<EmployeesContact> Contacts { get; set; }
public ObservableCollection<EmployeesDuty> Duties { get; set; }
public ObservableCollection<EmployeesLanguage> Languages { get; set; }
}
public class EmployeesContact
{
[Key] // Define TelephoneID as the primary key
public int TelephoneID { get; set; }
public int EmployeeID { get; set; }
public string Telephone { get; set; }
// Other properties and relationships
}
public class EmployeesDuty
{
public int EmployeeID { get; set; }
[Key]
public int EmployeeDutyID { get; set; }
public string Duty { get; set; }
}
public class EmployeesLanguage
{
public int EmployeeID { get; set; }
[Key]
public int EmployeeLanguageID { get; set; }
public string LanguageName { get; set; }
public string TalkingAbility { get; set; }
public string ReadingAbility { get; set; }
public string WritingAbility { get; set; }
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.