Articles in this section
Category / Section

How to delete a row when long pressing on a grid cell

1 min read

SfDataGrid fires the SfDataGrid.GridLongPressed event when the grid is long pressed. You can perform any desired operation upon long pressing by handling the GridLongPressed event. The GridLongPressed event provides you with the arguments “RowIndex” and “RowData” (the underlying data associated with the row based on the row type) there by allowing you to customise the long press event based on the requirements.

The SfDataGrid fires this event for all type of rows such as header row, caption summary rows and data rows. When you long press the header row the associated GridColumn of the long pressed header cell is passed as RowData. When you long press the data rows, the underlying data object associated with the long pressed row is passed as RowData. When you long press the caption rows, the group information associated with the long pressed caption row is passed as RowData. Thus by checking whether the RowData is the underlying data object you can delete the respective row in SfDataGrid using the RowColumnIndex or the RowData.

Refer the following example which illustrates how to delete a row in a SfDataGrid when long pressing it by handling the GridLongPressed event.

public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SfDataGrid sfGrid = new SfDataGrid(this);
        sfGrid.ItemsSource = new ViewModel().ProductDetails;
        sfGrid.GridLongPressed += sfGrid_GridLongPressed;
        SetContentView(sfGrid);
    }
 
    void sfGrid_GridLongPressed(object sender, GridLongPressedEventsArgs e)
    {
        if (e.RowData is ProductInfo)
        {
            viewModel.ProductDetails.RemoveAt(e.RowColumnindex.RowIndex - 1);
            Toast.MakeText(this, "Row " + e.RowColumnindex.RowIndex.ToString() + " Deleted!", ToastLength.Short).Show();
        }
    }
}

 

The following screenshot shows the final outcome upon execution of the above code.

 

 


 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied