Articles in this section
Category / Section

How to get the state of a checkbox while editing in .NET MAUI DataGrid (SfDataGrid) ?

5 mins read

In this article, we will demonstrate how to get the state of a checkbox while editing in the .NET MAUI DataGrid.

xaml

<syncfusion:SfDataGrid x:Name="dataGrid" 
                       AutoGenerateColumnsMode="None"
                       ColumnWidthMode="Auto"
                       GridLinesVisibility="Both"
                       HeaderGridLinesVisibility="Both"
                       ItemsSource="{Binding Employees}">
    <syncfusion:SfDataGrid.Columns>
        <syncfusion:DataGridCheckBoxColumn MappingName="EmployeeStatus"
                                           HeaderText="Employee Status" />
        <syncfusion:DataGridTextColumn MappingName="EmployeeID"
                                       HeaderText="Employee ID" />
        <syncfusion:DataGridTextColumn MappingName="Name"
                                       HeaderText="Employee Name" />
        <syncfusion:DataGridTextColumn MappingName="Title"
                                       HeaderText="Designation" />
    </syncfusion:SfDataGrid.Columns>

</syncfusion:SfDataGrid>

C#
The code below illustrates how to get the state of a checkbox while editing in the DataGrid.

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        this.dataGrid.CellRenderers.Remove("CheckBox");
        dataGrid.CellRenderers.Add("CheckBox", new DataGridCheckBoxCellRendererExt());
    }
}

public class DataGridCheckBoxCellRendererExt : DataGridCheckBoxCellRenderer
{
    protected override void OnInitializeDisplayView(DataColumnBase dataColumn, StackLayout? view)
    {
        base.OnInitializeDisplayView(dataColumn, view);

        if (view != null && view.Children[0] is CheckBox checkBox)
        {
            checkBox.PropertyChanging += DataGridCheckBoxCellRendererExt_PropertyChanging;

            checkBox.PropertyChanged += DataGridCheckBoxCellRendererExt_PropertyChanged;
        }
    }

    private async void DataGridCheckBoxCellRendererExt_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        var checkBox = sender as CheckBox;

        if (e.PropertyName == "IsChecked" && checkBox != null && checkBox.IsFocused && !DataGrid!.View!.IsEditingItem)
        {
            var isChecked = (sender as CheckBox)?.IsChecked ?? false;

            // Display an alert dialog
            await Application.Current.MainPage.DisplayAlert(
                "Checkbox State Changed",
                $"IsChecked: {isChecked}",
                "OK"
            );
        }
    }

    private async void DataGridCheckBoxCellRendererExt_PropertyChanging(object sender, PropertyChangingEventArgs e)
    {
        var checkBox = sender as CheckBox;

        if (e.PropertyName == "IsChecked" && checkBox != null && checkBox.IsFocused && !DataGrid!.View!.IsEditingItem)
        {
            var isChecked = (sender as CheckBox)?.IsChecked ?? false;

            // Display an alert dialog
            await Application.Current.MainPage.DisplayAlert(
                "Checkbox State Changed",
                $"IsChecked: {isChecked}",
                "OK"
            );
        }
    }
}

Output

CheckBoxState.gif

Download the complete sample from GitHub.

Conclusion

I hope you enjoyed learning how to get the state of a checkbox while editing in .NET 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 on the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to explore 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 forums, Direct-Trac or feedback portal, or the 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