Here's an article I found on the web to hroupe datas in a WPF Datagrid : Grouping in DataGrid in WPF
I want to test it in a WPF application with data coming from an Entity Framework DataContext
.
First I want to check that data are correctly implemented in the DataGrid
.
But the DataGrid
stays empty.
Here's may xaml :
<UserControl x:Class="GESTION_CONGELATION_V2.User_Controls.ViewPvi_UC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<DataGrid x:Name="Pvi_DT">
</DataGrid>
</UserControl>
My .cs
public partial class ViewPvi_UC : UserControl
{
ObservableCollection<VW_PVI_2> pvi = new ObservableCollection<VW_PVI_2>();
public ViewPvi_UC()
{
InitializeComponent();
ObservableCollection<VW_PVI_2> pvi = new ObservableCollection<VW_PVI_2>();
Pvi_DT.ItemsSource = pvi;
}
}
and my Entity Framework class
public partial class VW_PVI_2
{
public int PVI_ID { get; set; }
public string VE_NOM { get; set; }
public string VD_VIRTUAL { get; set; }
public string VE_OLD_NAME { get; set; }
public string ST_LIB { get; set; }
public string VE_COMM { get; set; }
public string IMP_NOM { get; set; }
public string IMP_IP { get; set; }
public string IMP_MAC { get; set; }
public string ST_LIB_IMP { get; set; }
public string IMP_COMM { get; set; }
public string SERV_NOM { get; set; }
public string PO_NOM { get; set; }
}
You can change your the pvi
declaration as following, to use the ICollectionView
:
public partial class ViewPvi_UC : UserControl
{
public ICollectionView pvi { get; set; }
public ViewPvi_UC()
{
InitializeComponent();
}
async private void Window_Loaded(object sender, RoutedEventArgs e)
{
await Task<object>.Factory.StartNew(() =>
{
// Select all `VW_PVI_2` from your table here to be populate the `pvi`
IList<VW_PVI_2> data= YourDbContext.YourTable.Select(r => r).ToList();
pvi = new ObservableCollection<VW_PVI_2>(data);
this.Dispatcher.Invoke(() => { Pvi_DT.ItemsSource = pvi; });
return Task.FromResult<object>(null);
});
}
}
Adding the Loaded
event in the XAML:
<UserControl ...
d:DesignHeight="450" d:DesignWidth="800"
Loaded="Window_Loaded">
<DataGrid x:Name="Pvi_DT">
</DataGrid>
</UserControl>
Loading the data from a table is moved to the Loaded
event handler and performing asynchronously. Because of loading data can take some time. Therefore is is possible to implement some logic to show a message that loading data is performing to indicate the user about this process.
In case you want to hide some columns (for example PVI_ID
) it is possible to use a custom attribute:
public class HiddenAttribute : Attribute
{
}
public partial class VW_PVI_2
{
[Hidden]
public int PVI_ID { get; set; }
...
}
Then add the AutoGeneratingColumn
event handler declaration to the dataGrid
to the XAML:
<DataGrid x:Name="dataGrid1" AutoGeneratingColumn="Pvi_DT_AutoGeneratingColumn"/>
And the handler declaration to hide some columns:
private void Pvi_DT_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyDescriptor is PropertyDescriptor prop && prop.Attributes.OfType<HiddenAttribute>().Any())
{
e.Cancel = true;
}
}