Articles in this section
Category / Section

How to pass selected row's data to a command?

1 min read

SfDataGrid provides two events for selection: SelectionChanging and SelectionChanged. It provides the added items and the removed items as arguments in it. You can pass the selected row info to a command in the ViewModel by using the Xamarin.Forms.Behaviors.

Refer the below code example in which a custom behavior for passing the selected row information to ViewModel is defined.

<?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns=""







    <local:ViewModel x:Name="viewModel" />





      <local:CustomConverter x:Key="gridSelectionChangedEventArgs" />




  <syncfusion:SfDataGrid x:Name="dataGrid"



                     ItemsSource="{Binding OrderInfo}">


      <local:CustomBehavior Command="{Binding SelectionCommand}"

                            Converter="{StaticResource gridSelectionChangedEventArgs}" />





The below code illustrates the command and the call back implementation to get the selected row information.

public class ViewModel
        private ICommand selectionCommand;
        public ICommand SelectionCommand
            get { return selectionCommand; }
            set { selectionCommand = value; }
        public ViewModel()
            selectionCommand = new Command<GridSelectionChangedEventArgs>(OnSelectionChanged);
            OrderInfoRepository orderInfoRepository = new OrderInfoRepository();
            orderInfo = orderInfoRepository.GetOrderDetails(30);
        private ObservableCollection<OrderInfo> orderInfo;
        public ObservableCollection<OrderInfo> OrderInfo
            get { return orderInfo; }
            set { this.orderInfo = value; }
        internal void GenerateItemsSource(int count)
            OrderInfoRepository order = new OrderInfoRepository();
            orderInfo = order.GetOrderDetails(count);
        private void OnSelectionChanged(GridSelectionChangedEventArgs e)
            var SelectedItem = e.AddedItems;



The below code illustrates the behavior class implementation for the SfDataGrid.

public class CustomBehavior : Behavior<SfDataGrid>
        public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(CustomBehavior), null);
        public static readonly BindableProperty InputConverterProperty = BindableProperty.Create("Converter", typeof(IValueConverter), typeof(CustomBehavior), null);
        public ICommand Command
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        public IValueConverter Converter
            get { return (IValueConverter)GetValue(InputConverterProperty); }
            set { SetValue(InputConverterProperty, value); }
        public SfDataGrid AssociatedObject { get; private set; }
        protected override void OnAttachedTo(SfDataGrid bindable)
            AssociatedObject = bindable;
            bindable.BindingContextChanged += OnBindingContextChanged;
            bindable.SelectionChanged += bindable_SelectionChanged;
        void bindable_SelectionChanged(object sender, GridSelectionChangedEventArgs e)
            if (Command == null)
            object parameter = Converter.Convert(e, typeof(object), null, null);
            var temp = (parameter as GridSelectionChangedEventArgs).AddedItems;
            if (Command.CanExecute(parameter))
        protected override void OnDetachingFrom(SfDataGrid bindable)
            bindable.BindingContextChanged -= OnBindingContextChanged;
            bindable.SelectionChanged -= bindable_SelectionChanged;
            AssociatedObject = null;
        void OnBindingContextChanged(object sender, EventArgs e)
        protected override void OnBindingContextChanged()
            BindingContext = AssociatedObject.BindingContext;




Sample Link:

How to pass selected row info to a command?

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