Problem In WPF datagrid editing with grouping enable
Datagrid xaml code:
<controls:DataGrid Name="dataGrid" AutoGenerateColumns="False" >
<controls:DataGrid.GroupStyle>
<开发者_如何学JAVA;GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</controls:DataGrid.GroupStyle>
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Header="Student ID" Width="90*" MinWidth="120" Binding="{Binding StudentId}"/>
<controls:DataGridTextColumn Header="Student Name" Width="90*" MinWidth="120" Binding="{Binding Name}"/>
<controls:DataGridTextColumn Header="Score" Width="100*" MinWidth="150" Binding="{Binding Score}"/>
</controls:DataGrid.Columns>
</controls:DataGrid>
Here the code behind:
void LoadDatagrid()
{
List<Student> _studentList = new List<Student>();
_studentList.Add(new Student()
{
StudentId = 1,
Name = "Paul Henriot",
Department = "IT",
Score = 540
});
_studentList.Add(new Student()
{
StudentId = 2,
Name = "John Doe",
Department = "IT",
Score = 620
});
_studentList.Add(new Student()
{
StudentId = 3,
Name = "Aria Cruz",
Department = "ME",
Score = 840
});
_studentList.Add(new Student()
{
StudentId = 4,
Name = "Yoshi Latimer",
Department = "ME",
Score = 450
});
CollectionViewSource viewSource = new CollectionViewSource();
viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Department"));
viewSource.Source = _studentList; ;
dataGrid.ItemsSource = viewSource.View;
}
public class Student
{
public int StudentId{ get; set; }
public string Name { get; set; }
public string Department { get; set; }
public int Score { get; set; }
}
when i am trying to edit the score or name in first any one of the department,after editing that row jumps to down.
Need a help on this.
I think your code is wrong, the binding path of the GroupStyle should be "Department", not "Name".
Change your code from:
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
to this:
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Department}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
It should work fine.
This Solves my problem,I have added SortDescription to CollectionViewSource.
CollectionViewSource viewSource = new CollectionViewSource();
viewSource.GroupDescriptions.Add(new PropertyGroupDescription("Department"));
viewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("Department", System.ComponentModel.ListSortDirection.Ascending));
viewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("StudentId", System.ComponentModel.ListSortDirection.Ascending));
viewSource.Source = _studentList; ;
dataGrid.ItemsSource = viewSource.View;
精彩评论