How to clear grouping when long pressing on a caption row?
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 a “Group” you can clear the grouping in SfDataGrid by clearing the SfDataGrid.GroupColumnDescriptions collection.
Refer the following example which illustrates clearing the grouping in a SfDataGrid when long pressing a caption row by handling the GridLongPressed event.
public partial class ViewController : UIViewController { SfDataGrid grid= new SfDataGrid(); ViewModel viewModel=new ViewModel(); public override void ViewDidLoad () { base.ViewDidLoad (); View.BackgroundColor = UIColor.White; grid.ItemsSource = viewModel.ProductDetails; grid.BackgroundColor = UIColor.White; grid.GroupColumnDescriptions.Add(new GroupColumnDescription() { ColumnName = "UserRating" }); grid.GridLongPressed += grid_GridLongPressed; View.AddSubview (grid); } public override void ViewDidLayoutSubviews () { this.grid.Frame = new CGRect (0, 20, View.Frame.Width, View.Frame.Height); base.ViewDidLayoutSubviews (); } void sfGrid_GridLongPressed(object sender, GridLongPressedEventsArgs e) { //code to clear grouping if (e.RowData is Group) { UIAlertView alert = new UIAlertView () { Title = "Clear Grouping…", Message = "Are you sure to remove grouping from the grid ?" }; alert.AddButton ("OK"); alert.AddButton ("Cancel"); alert.Show (); alert.Clicked += Alert_Clicked; } } void Alert_Clicked (object sender, UIButtonEventArgs e) { if (e.ButtonIndex == 0) grid.GroupColumnDescriptions.Clear (); else return; } }
Refer the following screenshot for the final outcome upon execution of the above code
The working sample for this KB can be downloaded from the following link
https://www.syncfusion.com/downloads/support/directtrac/general/ze/LongPress511868129