DataGrid not scrolling smoothly

2019-07-19 07:07发布

问题:

I have a DataGrid which usually contains approx 30 rows it rarely goes above 200. My problem is when the user goes through the contents of the grid with the up/down arrow keys it keeps getting stuck(approx 10 to 40 seconds) and then jumping several rows at a time

My DataGrid

<DataGrid Name="DgInvoiceLines" KeyUp="DgInvoiceLines_KeyUp" BorderBrush="Black"
              RowBackground="Silver" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"
              AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Extended" CanUserAddRows="True" CanUserDeleteRows="True" 
              HorizontalContentAlignment="Center" CellEditEnding="DgInvoiceLines_CellEditEnding" VerticalContentAlignment="Center" 
              PreviewKeyDown="DgInvoiceLines_PreviewKeyDown" SelectionChanged="DgInvoiceLines_SelectionChanged" 
              CurrentCellChanged="DgInvoiceLines_CurrentCellChanged" ItemsSource="{Binding}" BorderThickness="0,2,0,0"
              EnableColumnVirtualization="False" EnableRowVirtualization="False" ScrollViewer.CanContentScroll="False">
        <DataGrid.Columns>
            <DataGridTextColumn Width="Auto" Header="ProductCode"  Binding="{Binding ProductCode}"/>
            <DataGridTextColumn Width="250" Header="Description"  Binding="{Binding Description}" FontSize="14"/>
            <DataGridTextColumn Width="61" Header="Inv_Quantity" Binding="{Binding Inv_Quantity}"/>
            <DataGridTextColumn Width="63" Header="Grn_Quantity" Binding="{Binding Grn_Quantity}"/>
            <DataGridTextColumn Width="59" Header="Inv_Price" Binding="{Binding Inv_Price}"/>
            <DataGridTextColumn Width="61" Header="Ord_Price" Binding="{Binding Grn_Price}"/>
            <DataGridTextColumn Width="72" Header="Inv_Total" Binding="{Binding Inv_Total}"/>
            <DataGridTextColumn Width="74" Header="Grn_Total" Binding="{Binding Grn_Total}"/>
            <DataGridTextColumn Width="58" Header="AnalCode" Binding="{Binding AnalCode}"/>
            <DataGridTextColumn Width="60" Header="Vat_Rate" Binding="{Binding Vat_Rate}"/>
            <DataGridTextColumn Width="60" Header="Vat" Binding="{Binding Vat}" IsReadOnly="True"/>
            <DataGridTextColumn Width="Auto" Header="GrnNo"  Binding="{Binding GrnNo}"/>
            <DataGridCheckBoxColumn  Width="Auto" Binding="{Binding Line_Correct}" Header="" >
                <DataGridCheckBoxColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
                    </Style>
                </DataGridCheckBoxColumn.CellStyle>
            </DataGridCheckBoxColumn>
            <DataGridTextColumn Width="Auto" Header="Comment" Binding="{Binding Comment}"/>
            <DataGridTextColumn Width="Auto" Header="PerP" Binding="{Binding OuterUnits}" IsReadOnly="True"/>
        </DataGrid.Columns>
        <DataGrid.CellStyle >
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="IsKeyboardFocused"  Value="True" >
                            <Setter Property="Background" Value="White" />
                            <Setter Property="Foreground" Value="Black" />
                        </Trigger>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Foreground" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
        </DataGrid.CellStyle>
        <DataGrid.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightSteelBlue"/>
        </DataGrid.Resources>
    </DataGrid>

It's not using Entity Framework, and the data is on a very busy virtual server.

Can anyone point to somewhere to look for a solution or indicate any problems in my XAML?

EDIT

I'm starting to think that I'm just trying to get the grid to do too much!

回答1:

you disable EnableRowVirtualization and EnableColumnVirtualization and also you do something in PreviewKeyDown="DgInvoiceLines_PreviewKeyDown"

this could slow down the grid scrolling



回答2:

DID NOT WORK:

Only thing I could guess is you triggers for the cells are firing while you scroll. This could slow down the scrolling. Try commenting out your triggers and see if that fixes the scrolling issue. Might not be what you want, but that is the only thing I can see that would slow down the scrolling.

The only other thing I've seen slow down the scrolling of a data grid in WPF is if there are images inside the rows.

APPROACH 2:

Another thing you can try is switching away from using a datagrid and just use a grid to display you data. Almost the same but the grid is a lot lighter of a control. I've never used the datagrid in WPF but I've heard that it is not very good on performance, so I just used the grid.

Good example of using a grid to display you data is at: http://msdn.microsoft.com/en-us/library/aa480224.aspx



回答3:

Is your DataGrid in a ScrollViewer? I had an issue where a DataGrid wouldn't virtualize its content because the ScrollViewer provided it with infinite space which it then used. So if this is the case, take it out of the ScrollViewer.

If you're not sure whether it virtualizes properly, you can download a memory profiler program (i.e. ANTS) and check how many instances of DataGrid cells are in existence at the time the performance goes down.