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

SfDataGrid right-clicking on multiple rows

Hello,

When I ctrl+click multiple rows on my sfDataGrid and then right-click, I lose the multiple row selection and instead only the row I right-clicked on is selected. Is there anyway to keep all the rows selected? I'm using syncfusion version 12.1.0.43 for .NET 4.0. I tried the following but it didn't work:

    public partial class MainWindow : Window
    {
        private RowViewModel[] _previewSelection;

        public MainWindow()
        {
            InitializeComponent();
            
            //this is to make the cell selection more Excel like.
            DataGrid.PreviewMouseRightButtonDown += HandlePreviewRightMouseDown;
            DataGrid.ContextMenuOpening += HandleContextMenuOpening;

            ...
        }

        
        private void HandlePreviewRightMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (DataGrid.SelectedItems.Any())
            {
                _previewSelection = DataGrid.SelectedItems.OfType<RowViewModel>().ToArray();
            }
            else
            {
                _previewSelection = new RowViewModel[0];
                DataGrid.ContextMenu = null;
            }
        }


        private void HandleContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            foreach (var selected in _previewSelection)
            {
                if (!DataGrid.SelectedItems.Contains(selected))
                {
                    DataGrid.SelectedItems.Add(selected);
                }
            }
        }

        ...
    }



2 Replies

JS Jayapradha S Syncfusion Team May 14, 2014 01:04 PM UTC

Hi Tahseen ,

 

We are able to reproduce the problem “Selection cleared when right click on the multiple selection of rows” and have logged defect report regarding this.

 

The fix for this issue will be included in our upcoming release Vol 2 which is scheduled in end of June 2014.

 

Please let usknow if you have any questions.

 

Regards,

Jayapradha



JS Jayapradha S Syncfusion Team May 16, 2014 12:22 PM UTC

Hi Tahseen,

 

We analyzed your requirement once again and you can achieve this by using the below code snippet.  

 

Code snippet:

this.sfDataGrid.PreviewMouseRightButtonDown += sfDataGrid_PreviewMouseRightButtonDown;

this.sfDataGrid.CurrentCellActivating+=sfDataGrid_CurrentCellActivating;

this.sfDataGrid.CurrentCellBeginEdit+=sfDataGrid_CurrentCellBeginEdit;


bool
rightbuttonpressed = false;

void sfDataGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)

{

    rightbuttonpressed = true;

}

 

private void sfDataGrid_CurrentCellActivating(object sender, Syncfusion.UI.Xaml.Grid.CurrentCellActivatingEventArgs args)

{

    if (rightbuttonpressed)

    {

        this.sfDataGrid.Dispatcher.BeginInvoke(new Action(() =>

        {

            rightbuttonpressed = false;

        }), DispatcherPriority.ApplicationIdle);

        args.Cancel = true;

    }

}

 

private void sfDataGrid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs args)

{

    if (rightbuttonpressed)

    {

        rightbuttonpressed = false;

        args.Cancel = true;

    }

}

 

We are assuming that your query is related to WPF SfDataGrid, but this forum placed under WinRT SfDataGrid. So could you please let us know the platform used whether it is WPF/Win RT?

 

Please let us know if this code snippet helps you.

 

Regards,

Jayapradha S


Loader.
Up arrow icon