Row Delete works only when GridSelectionUnit is Row

Hi, 

Does deleting a row with the Delete key only work when GridSelectionUnit is Row? It does not say this in the documentation but when I select Cell or Any, I am unable to delete rows with the Delete Key. I would like to be able to set the GridSelectionUnit to Any and still be able to delete rows.

Thanks, 
Aurea

4 Replies

SJ Sathiyathanam Jeyakumar Syncfusion Team April 18, 2018 10:13 AM UTC

Hi Aurea, 
 
We can able to delete the row by set the AllowDeleting property to true and  SelectionUnit property to Any. Find the code snippet for more information.  
You can set the property in code behind  
public MainWindow() 
{ 
    InitializeComponet(); 
 
   this.datagrid.AllowDeleting = true; 
   this.datagrid.SelctionUnit = Grid SelectionUnit.Any; 
} 
 
 
Also, we can make customization while pressing the delete key. Find the below code snippets for your reference.   
 
public class GridSelectionControllerExt : GridSelectionController 
    { 
        public GridSelectionControllerExt(SfDataGrid dataGrid) : base(dataGrid) 
        { 
 
        } 
         
        protected override void ProcessKeyDown(KeyEventArgs args) 
        { 
            //Customizes the Delete key operation. 
            if (args.Key == Key.Delete) 
            { 
                this.DataGrid.View.Records.RemoveAt(this.DataGrid.SelectedIndex); 
            } 
            else 
            { 
                base.ProcessKeyDown(args); 
            } 
        } 
         
    } 
 
Code behind: 
public MainWindow() 
{ 
    InitializeComponet(); 
    // Assigns the customized GridSelectionControllerExt. 
    this.dataGrid.SelectionController = new GridSelectionControllerExt(dataGrid);  
} 
 
 
 
If the issue is still persisting, could you please provide the sample to us based on your application, this will help us to analyze further and provide the better solution to you. 
 
Regards, 
Sathiyathanam 



AD Aurea Del Moral April 18, 2018 04:00 PM UTC

Sathiyathanam,

I tried adapting the code provided. The only outcome was that my SelectionUnit was overriden to Row instead of Any.
Also, I noticed the demo project attached was WPF (xmlns:syncfusion ="http://schemas.syncfusion.com/wpf"). I am working this out in UWP.

Will continue looking into this and try to provide more pointed questions, but if you have something more that can help, it would be appreciated. 

Thanks, 
Aurea


AD Aurea Del Moral April 18, 2018 08:10 PM UTC

I was able to get delete working with GridCellSelectionController instead. 

However, when a row is deleted, the SfDataGrid.RecordDeleting event is not triggered. 
Is there anything else I need to include in protected override void ProcessKeyDown(KeyEventArgs args)
to ensure the event is triggered?


SJ Sathiyathanam Jeyakumar Syncfusion Team April 19, 2018 12:45 PM UTC

Hi Aurea, 

Please ignore the previous update. 

We can able to delete the row when the SelectionUinit is row. But we can’t able to delete the row when SelectionUnit is Cell or Any and it’s default behavior of SfDataGrid. So, you can make the customization for deleting the row when the SelctionUnit is Any or Cell using the following code snippets. 
 
public MainWindow() 
{ 
    InitializeComponet(); 
 
   this.datagrid.AllowDeleting = true; 
   this.datagrid.SelctionUnit = Grid SelectionUnit.Any; 
   this.dataGrid.SelectionController = new GridSelectionControllerExt(dataGrid);  
 
} 
 
 
public class GridSelectionControllerExt : GridSelectionController 
    { 
        public GridSelectionControllerExt(SfDataGrid dataGrid) : base(dataGrid) 
        { 
 
        } 
         
        protected override void ProcessKeyDown(KeyEventArgs args) 
        { 
            //Customizes the Delete key operation. 
            if (args.Key == Key.Delete && (this.DataGrid.SelectionUnit == GridSelectionUnit.Any || this.DataGrid.SelectionUnit == GridSelectionUnit.Cell)) 
            { 
                this.DataGrid.View.Records.RemoveAt(this.DataGrid.SelectedIndex); 
            } 
            else 
            { 
                base.ProcessKeyDown(args); 
            } 
        } 
         
    } 
 
Please find the sample from below location. 
 
RecordDeleting Event occurs when the record is being deleted from SfdataGrid. 
You can get details about RecordDeleting event and RecordDeleted event from the below link. 

 
 
 
Regards, 
Sathiyathanam 


Loader.
Up arrow icon