How to pass selected row's data to a command in MAUI DataGrid (SfDataGrid)?
The .NET MAUI DataGrid (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.
Refer the below code example in which a custom behavior for passing the selected row information to ViewModel is defined.
XAML
<syncfusion:SfDataGrid x:Name="dataGrid" SelectionMode="Single" ItemsSource="{Binding OrderInfo}"> <syncfusion:SfDataGrid.Behaviors> <local:CustomBehavior Command="{Binding SelectionCommand}" Converter="{StaticResource gridSelectionChangedEventArgs}" /> </syncfusion:SfDataGrid.Behaviors> <syncfusion:SfDataGrid.Columns> <syncfusion:DataGridNumericColumn MappingName="OrderID" HeaderText="Order ID" Format="d"/> <syncfusion:DataGridTextColumn MappingName="EmployeeID" HeaderText="Employee ID"/> <syncfusion:DataGridTextColumn MappingName="CustomerID" HeaderText="Customer ID"/> <syncfusion:DataGridTextColumn MappingName="FirstName" HeaderText="First Name"/> <syncfusion:DataGridTextColumn MappingName="LastName" HeaderText="Last Name"/> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid>
C#
// ViewModel public class ViewModel { private ICommand selectionCommand; public ICommand SelectionCommand { get { return selectionCommand; } set { selectionCommand = value; } } public ViewModel() { selectionCommand = new Command<DataGridSelectionChangedEventArgs>(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(DataGridSelectionChangedEventArgs e) { var SelectedItem = e.AddedRows; } }
CustomBehavior
// Behavior 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) { base.OnAttachedTo(bindable); AssociatedObject = bindable; bindable.BindingContextChanged += OnBindingContextChanged; bindable.SelectionChanged += Bindable_SelectionChanged; } private void Bindable_SelectionChanged(object sender, DataGridSelectionChangedEventArgs e) { if (Command == null) return; object gridSelectionChangedEventArgs = Converter.Convert(e, typeof(object), null, null); if (Command.CanExecute(gridSelectionChangedEventArgs)) Command.Execute(gridSelectionChangedEventArgs); } protected override void OnDetachingFrom(SfDataGrid bindable) { base.OnDetachingFrom(bindable); bindable.BindingContextChanged -= OnBindingContextChanged; bindable.SelectionChanged -= Bindable_SelectionChanged; AssociatedObject = null; } private void OnBindingContextChanged(object sender, EventArgs e) { OnBindingContextChanged(); } protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); BindingContext = AssociatedObject.BindingContext; } } public class CustomConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
Take a moment to pursue this documentation, where you can find more about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples.
Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid).
Conclusion
I hope you enjoyed learning about how to pass selected row’s data to a command in MAUI DataGrid (SfDataGrid).
You can refer to our .NET MAUI DataGrid’s feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!