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
close icon

Delete button or something else?

Hello, is it possible to delete a row besides the delete key? like binding to a button, confirmation pop-up message on delete key or anything else?



6 Replies

JP Jagadeesan Pichaimuthu Syncfusion Team May 20, 2019 03:14 PM UTC

Hi Arthur, 
 
Thanks for using Syncfusion product. 
 
The selected records can be deleted when clicking the cell button by using the DeleteSelectedRecords method in the SfDataGrid.CellButtonClick event. Please refer the below documentation link for your reference, 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Regards, 
Jagadeesan 



AR Arthur May 20, 2019 04:25 PM UTC

I got a System.NullReferenceException: 'object reference not set to an instance of an object.'


I'm using DataSource for my grid table does it affect something?


AR Arthur May 20, 2019 04:40 PM UTC

Ok, i'm not getting an exception anymore, don't know if i did it right: Should I use "this.sfDataGrid1.CellButtonClick += Double2Click;" on the Load method or under "InitializeComponent()" ?

but now when I click it, it doesn't do anything
 


JP Jagadeesan Pichaimuthu Syncfusion Team May 21, 2019 09:17 AM UTC

Hi Arthur, 
Thanks for your update. 
 
We have checked the provided link, but it “Page Not Found” error occurs while open the provided link. And we have tried to reproduce the reported problem in our end with button click operation, but it works properly in our end. Please find the below code snippet, 
// Add the below code in constructor or form loading  
  
this.sfDataGrid.AllowDeleting = true;  
  
this.sfDataGrid.Columns.Add(new GridButtonColumn()  
{  
    MappingName = "Quantity",  
    HeaderText = "Remove",  
    AllowDefaultButtonText = true,  
    DefaultButtonText = "Remove this row"  
});  
  
this.sfDataGrid.CellButtonClick += sfDataGrid_CellButtonClick;  
  
  
void sfDataGrid_CellButtonClick(object sender, Syncfusion.WinForms.DataGrid.Events.CellButtonClickEventArgs e)  
{  
    int _Dgdeletingrowindex = sfDataGrid.TableControl.ResolveToRecordIndex(sfDataGrid.CurrentCell.RowIndex);  
  
    // Delete record by based on the row index  
    sfDataGrid.View.RemoveAt(_Dgdeletingrowindex);  
  
   // Or you can call the below code the delete the selected record  
   // this.sfDataGrid.DeleteSelectedRecords();  
}  
 
 
Please find the sample from below location. 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Regards, 
Jagadeesan


AR Arthur May 22, 2019 12:01 PM UTC



JP Jagadeesan Pichaimuthu Syncfusion Team May 23, 2019 01:07 PM UTC

Hi Artur, 
 
Thanks for your update. 
 
We have checked the provided sample code in our end, but we could not find any code related to creating the button column. The CellButtonClick event will be fired only when click on the button cell(any cell in button column). You can able to add the button column to SfDataGrid in two ways. 
 
First way : If AutoGenerateColumns is false: 
 
When AutoGeneratedColumns is false, you need to create and add the columns by yourself. Please refer the below code snippet for adding the Button column in SfDataGrid. 
 
// Under form loading method 
this.sfDataGrid.AllowDeleting = true; 
this.sfDataGrid.AutoGenerateColumns = false; 
sfDataGrid.DataSource = data.OrdersListDetails; 
 
this.sfDataGrid.Columns.Add(new GridButtonColumn() 
{ 
    MappingName = "Quantity", 
    HeaderText = "Remove", 
    AllowDefaultButtonText = true, 
    DefaultButtonText = "Remove this row" 
}); 
 
this.sfDataGrid.CellButtonClick += sfDataGrid_CellButtonClick; 
 
 
 
Second way: If AutoGenerateColumns is true: 
 
When AutoGenerateColumns is true, you can able to initialize the button column using the AutoGeneratingColumn for particular column. Please find the code snippet to add the button column below, 
 
// Under form loading method 
this.sfDataGrid.AllowDeleting = true; 
this.sfDataGrid.AutoGenerateColumns = true; 
this.sfDataGrid.AutoGeneratingColumn += sfDataGrid_AutoGeneratingColumn; 
sfDataGrid.DataSource = data.OrdersListDetails; 
 
 
void sfDataGrid_AutoGeneratingColumn(object sender, Syncfusion.WinForms.DataGrid.Events.AutoGeneratingColumnArgs e) 
{ 
    if (e.Column.MappingName == "Quantity") 
    { 
        GridButtonColumn column = new GridButtonColumn() 
        { 
            MappingName = "Quantity", 
            HeaderText = "Remove", 
            AllowDefaultButtonText = true, 
            DefaultButtonText = "Remove this row" 
        }; 
        e.Column = column; 
    } 
} 
 
Once you added the button column you can wire the CellButtonClick event. Also ensure the AllowEditing is true for SfDataGrid control. We have prepared the simple sample and video for your reference. 
 
 
Let us know whether this helps also if you need any further assistance on this. 
 
Regards, 
Jagadeesan 


Loader.
Live Chat Icon For mobile
Up arrow icon