How to add button column in MAUI DataGrid (SfDataGrid)?
The .NET MAUI DataGrid(SfDataGrid) allows loading of any views in the DataGridTemplateColumn. In this article, you can learn how to load the button for a specific column and perform any action on the button click.
C#
Create the ButtonCommand using execute method to delete the current row when performing the click action on the button.
public class ViewModel : INotifyPropertyChanged { private ObservableCollection<OrderInfo> orderInfo; private ICommand buttonCommand; public ObservableCollection<OrderInfo> OrderInfoCollection { get { return orderInfo; } set { this.orderInfo = value; RaisePropertyChanged("OrderInfoCollection"); } } public ICommand ButtonCommand { get { return buttonCommand; } set { buttonCommand = value; } } public ViewModel() { this.OrderInfoCollection = new ObservableCollection<OrderInfo>(); this.GenerateOrders(); this.ButtonCommand = new Command(DeleteRecord); } public void GenerateOrders() { orderInfo.Add(new OrderInfo(1001, "Ana Trujillo", "Mexico", "ANATR")); orderInfo.Add(new OrderInfo(1002, "Ant Fuller", "Mexico", "ANTON")); orderInfo.Add(new OrderInfo(1003, "Thomas Hardy", "UK", "AROUT")); orderInfo.Add(new OrderInfo(1004, "Tim Adams", "Sweden", "BERGS")); orderInfo.Add(new OrderInfo(1005, "Hanna Moos", "Germany", "BLAUS")); orderInfo.Add(new OrderInfo(1006, "Andrew Fuller", "France", "BLONP")); orderInfo.Add(new OrderInfo(1007, "Martin King", "Spain", "BOLID")); orderInfo.Add(new OrderInfo(1008, "Lenny Lin", "France", "BONAP")); orderInfo.Add(new OrderInfo(1009, "John Carter", "Canada", "BOTTM")); orderInfo.Add(new OrderInfo(1010, "Laura King", "UK", "AROUT")); orderInfo.Add(new OrderInfo(1011, "Ant Fuller", "Mexico", "ANTON")); orderInfo.Add(new OrderInfo(1012, "Thomas Hardy", "UK", "AROUT")); orderInfo.Add(new OrderInfo(1013, "Tim Adams", "Sweden", "BERGS")); orderInfo.Add(new OrderInfo(1014, "Hanna Moos", "Germany", "BLAUS")); orderInfo.Add(new OrderInfo(1015, "Andrew Fuller", "France", "BLONP")); orderInfo.Add(new OrderInfo(1016, "Martin King", "Spain", "BOLID")); orderInfo.Add(new OrderInfo(1017, "Lenny Lin", "France", "BONAP")); orderInfo.Add(new OrderInfo(1018, "John Carter", "Canada", "BOTTM")); orderInfo.Add(new OrderInfo(1019, "Laura King", "UK", "AROUT")); } private void DeleteRecord(object orderInfo) { this.OrderInfoCollection.Remove(orderInfo as OrderInfo); } #region INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string name) { if (PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(name)); } #endregion }
XAML
Create a DataGridTemplateColumn with a button inside and bind a buttons’ Command property to the ViewModels’ ButtonCommand property.
<syncfusion:SfDataGrid x:Name="dataGrid" AutoGenerateColumnsMode="None" ItemsSource="{Binding OrderInfoCollection}" DefaultColumnWidth="130" GridLinesVisibility="Both" HeaderGridLinesVisibility="Both"> <syncfusion:SfDataGrid.Columns> <syncfusion:DataGridNumericColumn MappingName="OrderID" HeaderText="ID"></syncfusion:DataGridNumericColumn> <syncfusion:DataGridTextColumn MappingName="Customer" HeaderText="Name"></syncfusion:DataGridTextColumn> <syncfusion:DataGridTextColumn MappingName="Country"></syncfusion:DataGridTextColumn> <syncfusion:DataGridTemplateColumn MappingName="CustomerID"> <syncfusion:DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Text="Delete" Command="{Binding Source={x:Reference dataGrid},Path=BindingContext.ButtonCommand}" CommandParameter="{Binding .}"></Button> </DataTemplate> </syncfusion:DataGridTemplateColumn.CellTemplate> </syncfusion:DataGridTemplateColumn> </syncfusion:SfDataGrid.Columns> </syncfusion:SfDataGrid>
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.
Conclusion
I hope you enjoyed learning about how to add button column 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!