When sorting and grouping, the row numbering in the DataGrid table gets confused.

enter image description here

<Window.DataContext> <local:MainViewModel/> </Window.DataContext> <Grid> <DataGrid x:Name="DGR" AutoGenerateColumns="False" ItemsSource="{Binding AllEmployee}" Margin="0,0,0,10"> <DataGrid.Columns> <DataGridTextColumn x:Name="ID" Binding="{Binding id}" Header="№"/> <DataGridTextColumn x:Name="Position" Binding="{Binding position}" Header="Фамилия"/> <DataGridTextColumn x:Name="Unit" Binding="{Binding unit}" Header="Группа"/> </DataGrid.Columns> <DataGrid.GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle> <Style TargetType="GroupItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <StackPanel> <TextBlock FontWeight="Bold" Foreground="Black" FontSize="12" TextAlignment="Center" Text="{Binding Path=Name}"/> <TextBlock Foreground="Blue" FontSize="12" FontWeight="Bold" Text="{Binding ItemCount, StringFormat=Количество сотрудников в группе: {0}}" Margin="30,0,0,5" /> <ItemsPresenter/> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </GroupStyle.ContainerStyle> </GroupStyle> </DataGrid.GroupStyle> </DataGrid> </Grid> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ICollectionView cvTasks = CollectionViewSource.GetDefaultView(DGR.ItemsSource); if(cvTasks != null && cvTasks.CanGroup == true) { cvTasks.GroupDescriptions.Clear(); cvTasks.SortDescriptions.Clear(); cvTasks.GroupDescriptions.Add(new PropertyGroupDescription("unit")); cvTasks.SortDescriptions.Add(new SortDescription("unit", ListSortDirection.Ascending)); } } } public class MainViewModel { public ObservableCollection<Employee> AllEmployee { get; set; } public MainViewModel() { this.Initialize(); } private void Initialize() { AllEmployee = new ObservableCollection<Employee>(); AllEmployee.CollectionChanged += OnGroceryListChanged; } void OnGroceryListChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { NumberService.SetCollection(this.AllEmployee); } } } public class Employee : ViewModelBase { private int ID { get; set; } private string Position { get; set; } private string Unit { get; set; } public int id { get { return this.ID; } set { if (value != ID) { ID = value; RaisePropertyChangedEvent("id"); } } } public string position { get { return Position; } set { if (value != Position) { Position = value; RaisePropertyChangedEvent("position"); } } } public string unit { get { return Unit; } set { if (value != Unit) { Unit = value; RaisePropertyChangedEvent("unit"); } } } } } public static class NumberService { public static ObservableCollection<T> SetCollection<T>(ObservableCollection<T> targetCollection) where T : Employee { var Number = 1; foreach (Employee sequencedObject in targetCollection) { sequencedObject.id = Number; Number++; } return targetCollection; } } } public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChangedEvent([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { var e = new PropertyChangedEventArgs(propertyName); PropertyChanged(this, e); } } } 

Thanks if you help in this issue.

  • What do you have in mind? It seems that the numbers were at the records - they remain. Or do you need a different behavior? - Andrew NOP
  • You already decide what you need. If you bind to Id , then this is exactly the Id in the collection, but if you need a sequence number in a subcollection sorted according to some rule, then bind to this parameter calculated in the course. - Bulson
  • In gif I indicated, for example, group number 1, number 2, etc. and the numbering order when the data was distributed among the groups turned out to be inconsistent, and not in order i.e. 1,2,3,4 and so on I made the group binding to the "Group" column cvTasks.GroupDescriptions.Add (new PropertyGroupDescription ("unit")); but how the numbering after the grouping is established in the sequence is not working for me. - Eugene
  • <DataGridTextColumn x:Name="ID" Binding="{Binding id}" Header="№"/> - what you wrote, it happens the same. Or do you want an entity ID to change during any operation? - Andrew NOP
  • If you just need a sequence number, then why not use your own converter: ru.stackoverflow.com/q/777851/218063 ? - Andrew NOP

0