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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Thread ID:

Created:

Updated:

Platform:

Replies:

131298 Jul 3,2017 05:28 PM UTC Aug 14,2017 12:48 PM UTC WPF 3
loading
Tags: SfDataGrid
Nirmal Kumar Andrews
Asked On July 3, 2017 05:28 PM UTC

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".

Srinivasan Vasu [Syncfusion]
Replied On 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 


Nirmal Kumar Andrews
Replied On 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

Balamurugan Rajaraman [Syncfusion]
Replied On 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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

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

;