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

delete key does not delete the rows when the SelectionUnit="Any" and SelectionMode="Extended"

Hi,

I need to delete an entire row by clicking on the row header which selects the entire row and press "delete" key.

This does not work. Is there any other way to achieve this. without changing the  SelectionUnit="Any" and SelectionMode="Extended".

3 Replies

SV Srinivasan Vasu Syncfusion Team July 4, 2017 01:06 PM UTC

Hi Nirmal, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query and we have prepared a sample as per your scenario. You can achieve your requirement by overriding ProcessKeyDown method in GridCellSelectionController. 
 
Please refer the below code example 
 
this.dataGrid.SelectionController = new GridSelectionControllerExt(this.dataGrid);          
 
public class GridSelectionControllerExt: GridCellSelectionController 
    {         
        public GridSelectionControllerExt(SfDataGrid grid) : base(grid) 
        { 
 
        } 
        protected override void ProcessKeyDown(KeyEventArgs args) 
        { 
            base.ProcessKeyDown(args); 
            if (args.Key == Key.Delete) 
            {                 
                (this.DataGrid.DataContext as ViewModel).OrderInfoCollection.Remove(this.DataGrid.CurrentItem as Model); 
            } 
        }    
    } 
 
 
Regards,
Srinivasan 



NK Nirmal Kumar Andrews August 11, 2017 08:48 PM UTC

Hi,

Since all your examples involve view model format. it is very hard for me to understand.

I am new to C# and wpf application. And I not using the view model in code. Can you please help me how to implement the same in the WPFSyncFusionPayPeriods.rar code?






Attachment: WPFSyncFusionPayPeriods_55521fe8.rar


BR Balamurugan Rajaraman Syncfusion Team August 14, 2017 12:48 PM UTC

Hi Nirmal, 
 
We are sorry for the inconvenience.  
 
In your application, we have noticed that you are using the SQL connection to bind the ItemsSource in to SfDataGrid. So we suggest alternate way to remove that the particular record based upon the CurrentItem of the DataGrid in the ProcessKeyDown method as like below provided code sample. If you are using the ViewModel means kindly used our previous provided approach else follow the below way to meet your requirement. 
 
protected override void ProcessKeyDown(KeyEventArgs args) 
        { 
            base.ProcessKeyDown(args); 
            if (args.Key == Key.Delete) 
            { 
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Table1", connection); 
                try 
                { 
                    cmd = new SqlCommand("DELETE FROM Table1 (ID,Name) WHERE (@ID)", connection); 
                    cmd.Parameters.Add("@ID", SqlDbType.Int, 50, "ID"); 
                    SqlParameter parameter= cmd.Parameters.Add("@ID", SqlDbType.Int, 50, "ID"); 
                    da.DeleteCommand = cmd; 
                    da.Fill(DS); 
                    //To get the current position 
                    int index = this.DataGrid.ResolveToRowIndex(this.DataGrid.CurrentItem); 
                    int resolvedIndex = this.DataGrid.ResolveToRecordIndex(index); 
                    System.Data.DataRow dr = DS.Tables[1].Rows[resolvedIndex]; 
                    //It gives the details about the current item and then remove the remove the record from 
                    //the database based upon that record 
                    dr["ID"] = ((System.Data.DataRowView)this.DataGrid.CurrentItem).Row.ItemArray[0]; 
                    DS.Tables[1].Rows.Remove(dr); 
                    da.Update(DS); 
                } 
                catch 
                { 
 
                } 
                finally 
                { 
                    //Here you can code your view updation process 
                } 
            } 
        } 
 
Please let us know if you have any query on this. 
 
Regards, 
Balamurugan R 


Loader.
Live Chat Icon For mobile
Up arrow icon