Alternation index has a total bug

Lloyd Sheen 1,351 Reputation points

Using alternation index as a column in a datagrid is well ... buggy. On initial load the first page of data in the datagrid seems ok. But when scrolling down the bug appears. I use the alternation index as the first column of the datagrid as a "rank". If I scroll to the bottom of the datagrid it will show ranks 1 - #of visible rows. Scrolling up will show the alterternation index which is equal to the alternation count set for the datagrid. I can modify the alternation count to any value and no matter how many items are in the datagrid this will happen.

Not finished yet. The datagrid allows the user to sort based on clicking a header. When this happens well it gets worse Sometimes the first item will show as the rank the alternation count followed by values 1 thru the number of rows that can be shown. Scrolling thru the datagrid brings a combination of the above problems.

The datagrid I am talking about has approx. 1000 items. The display (other than the rank) and sorting works fine. Alternation index is simply wrong.

I have seen posts over the years about this bug and wonder if MS is doing anything about this or is there a way of doing what I am trying to do so that it works.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,666 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points

    Here is a workaround which use LoadingRow event to add the number for the items.You can implement it by adding the event LoadingRow="dataGrid_LoadingRow" for the DataGrid and generate the number in cs like below:

      private void dataGrid_LoadingRow(object sender, DataGridRowEventArgs e)  
                e.Row.Header = e.Row.GetIndex() + 1;  

    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.

1 additional answer

Sort by: Most helpful
  1. Lloyd Sheen 1,351 Reputation points

    BTW this is the XAML for the datagrid although this problem occurs in several test datagrids I have tried.

                            <DataGrid Grid.Row="2" ItemsSource="{Binding SkaterStats,NotifyOnSourceUpdated=True ,NotifyOnTargetUpdated=True}"
                                                CanUserAddRows="False" IsReadOnly="True" FrozenColumnCount="2" AlternationCount="3000"
                                              AutoGenerateColumns="False" Margin="0,0,0,20" Sorting="DataGrid_Sorting" TargetUpdated="DataGrid_TargetUpdated">
                                    <DataGridTextColumn Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Team" Binding="{Binding teamName}"></DataGridTextColumn>
                                    <DataGridTemplateColumn Header="Rank">
                                                <TextBlock   Background="Transparent" 
                                                Text="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=DataGridRow},Converter={StaticResource IAC}}">
                                    <DataGridTextColumn Header="Games" Binding="{Binding games}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Goals" Binding="{Binding goals}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Assists" Binding="{Binding assists}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Points" Binding="{Binding points}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="PIM" Binding="{Binding PIM}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="+/-" Binding="{Binding plusMinus}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="TOI" Binding="{Binding timeOnIce_t}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="PP TOI" Binding="{Binding powerPlaytimeOnIce_t}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="SH TOI" Binding="{Binding shortHandedTimeOnIce_t}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Even TOI" Binding="{Binding evenTimeOnIce_t}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Shots" Binding="{Binding shots}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Hits" Binding="{Binding hits}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="PP Goals" Binding="{Binding powerPlayGoals}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="PP Points" Binding="{Binding powerPlayPoints}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Faceoff %" Binding="{Binding faceOffPct}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Shot %" Binding="{Binding shotPct}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="GW Goals" Binding="{Binding gameWinningGoals}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="OT Goals" Binding="{Binding overTimeGoals}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="SH Goals" Binding="{Binding shortHandedGoals}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="SH Points" Binding="{Binding shortHandedPoints}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Blocked" Binding="{Binding blocked}"></DataGridTextColumn>
                                    <DataGridTextColumn Header="Shifts" Binding="{Binding shifts}"></DataGridTextColumn>
    0 comments No comments