We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to improve the performance while selecting row using shift key in WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid
Tags: selection

You can improve the performance of selecting the rows using Shift key by overriding the GridSelectionController.SelectRows method in WPF DataGrid (SfDataGrid).

The sample explains the performance improvement using Shift key.

SfDataGridBehavior.cs

public class SfDataGridBehavior : Behavior<SfDataGrid>
{
    SfDataGrid datagrid = null;
    protected override void OnAttached()
    {
        datagrid = this.AssociatedObject as SfDataGrid;
        datagrid.SelectionController = new GridSelectionControllerExt(datagrid);
        base.OnAttached();
    }
}

 

GridSelectionControllerExt.cs

public class GridSelectionControllerExt : GridSelectionController
{
    SfDataGrid datagrid;
    public GridSelectionControllerExt(SfDataGrid dataGrid): base(dataGrid)
    {
        datagrid = dataGrid;
    }
}

 

The ProcessPointerReleased override method is used to call the SelectRows method when pressing Shift key and releasing the pointer.

   /// <summary>
    /// Processes the row selection when the mouse pointer is released from SfDataGrid. 
    /// </summary>
    /// <param name="args">
    /// Contains the data for mouse pointer action.
    /// </param>
    /// <param name="rowColumnIndex">
    /// The corresponding rowColumnIndex of the mouse released point.
    /// </param>
    /// <remarks>
    /// The selection is initialized in pointer released state when the <see cref="Syncfusion.UI.Xaml.Grid.SfDataGrid.AllowSelectionPointerPressed"/> set as false.        
    /// </remarks>
    protected override void ProcessPointerReleased(MouseButtonEventArgs args, Syncfusion.UI.Xaml.ScrollAxis.RowColumnIndex rowColumnIndex)
    {
        if (SelectionHelper.CheckShiftKeyPressed() && this.PressedRowColumnIndex.RowIndex > 0)
        {
            this.SelectRows(this.PressedRowColumnIndex.RowIndex, rowColumnIndex.RowIndex);
        }
        else
            base.ProcessPointerReleased(args, rowColumnIndex);
    }
 
    /// <summary>
    /// Selects the rows corresponding to the specified start and end index of the row.
    /// </summary>
    /// <param name="startRowIndex">
    /// The start index of the row.
    /// </param>
    /// <param name="endRowIndex">
    /// The end index of the row.
    /// </param>
    public override void SelectRows(int startRowIndex, int endRowIndex)
    {
        if (startRowIndex < 0 || endRowIndex < 0)
            return;
 
        if (startRowIndex > endRowIndex)
        {
            var temp = startRowIndex;
            startRowIndex = endRowIndex;
            endRowIndex = temp;
        }
 
        if (this.DataGrid.SelectionMode == GridSelectionMode.None ||
            this.DataGrid.SelectionMode == GridSelectionMode.Single)
            return;
 
        var isSelectedRowsContains = this.SelectedRows.Any();
 
        this.SuspendUpdates();
        var addedItem = new List<object>();
        int rowIndex = startRowIndex;
 
        this.datagrid.SelectedItems.Clear();
        var rowIndexes = this.datagrid.SelectionController.SelectedRows.Select(rowinfo => rowinfo.RowIndex).ToList(); 
 
        var selectedrowindex = this.datagrid.ResolveToRowIndex(this.datagrid.SelectedIndex);
 
 
        for (int i = rowIndex; i <= endRowIndex; i++)
        {
            object rowData = this.DataGrid.GetRecordAtRowIndex(i);
 
            if (!rowIndexes.Contains(i))
            {
                this.SelectedRows.Add(GetGridSelectedRow(i));
                this.DataGrid.ShowRowSelection(i);
            }
            if (rowData != null)
            {
                this.DataGrid.SelectedItems.Add(rowData);
            }
        }
 
        if (!isSelectedRowsContains)
        {
            this.DataGrid.SelectedIndex = this.SelectedRows.Count > 0 ? this.DataGrid.ResolveToRecordIndex(this.SelectedRows[0].RowIndex) : -1;
            this.DataGrid.SelectedItem = this.DataGrid.SelectedItems.Count > 0 ? this.DataGrid.SelectedItems[0] : null;
 
        }
        this.ResumeUpdates();
    }
}

View sample in GitHub

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile