Articles in this section
Category / Section

How to add button column in MAUI DataGrid (SfDataGrid)?

3 mins read

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 the 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 the Button’s Command property to the ViewModel’s 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>

Download the complete sample from GitHub.

Conclusion

I hope you enjoyed learning how to add a button column in the MAUI DataGrid (SfDataGrid).

You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.

Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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