How to Implement Select All Checkbox Column in .NET MAUI DataGrid?
This article explains how to implement a Select All checkbox column in the Syncfusion .NET MAUI DataGrid (SfDataGrid). A checkbox will be placed in the header to allow selecting or deselecting all rows, while each row also has a checkbox for individual selection.
Xaml
<ContentPage.BindingContext>
<local:SampleViewModel x:Name="viewModel" />
</ContentPage.BindingContext>
<syncfusion:SfDataGrid x:Name="SampleGrid"
HeaderGridLinesVisibility="Both"
GridLinesVisibility="Both"
ItemsSource="{Binding Items}"
ColumnWidthMode="Auto"
AutoGenerateColumnsMode="None">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTemplateColumn HeaderText="Select All">
<syncfusion:DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<buttons:SfCheckBox x:Name="checkBox"
BindingContext="{Binding Source={x:Reference MyPage}, Path=BindingContext}"
IsThreeState="True"
IsChecked="{Binding SelectAllChecked, Mode=TwoWay}"
StateChanged="SfCheckBox_StateChanged_1" />
</DataTemplate>
</syncfusion:DataGridTemplateColumn.HeaderTemplate>
<syncfusion:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<buttons:SfCheckBox IsChecked="{Binding IsChecked}"
StateChanged="SfCheckBox_StateChanged" />
</DataTemplate>
</syncfusion:DataGridTemplateColumn.CellTemplate>
</syncfusion:DataGridTemplateColumn>
<syncfusion:DataGridCheckBoxColumn MappingName="SelectAllChecked"
HeaderText="SelectAll" />
<syncfusion:DataGridTextColumn HeaderText="Name"
MappingName="Name" />
<syncfusion:DataGridTextColumn HeaderText="Description"
MappingName="Description" />
<syncfusion:DataGridTextColumn HeaderText="Category"
MappingName="Category" />
<syncfusion:DataGridTextColumn HeaderText="Price"
MappingName="Price" />
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
Xaml.cs
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void SfCheckBox_StateChanged(object sender, Syncfusion.Maui.Buttons.StateChangedEventArgs e)
{
var viewModel = BindingContext as SampleViewModel;
if (viewModel != null && !viewModel._isUpdating)
{
viewModel._isUpdating = true;
if (viewModel.Items.All(x => x.IsChecked == true))
{
viewModel.SelectAllChecked = true;
}
else if (viewModel.Items.All(x => x.IsChecked == false))
{
viewModel.SelectAllChecked = false;
}
else
{
viewModel.SelectAllChecked = null;
}
viewModel._isUpdating = false;
}
}
private void SfCheckBox_StateChanged_1(object sender, StateChangedEventArgs e)
{
var viewModel = BindingContext as SampleViewModel;
if (viewModel != null)
{
if (!viewModel._isUpdating)
{
viewModel._isUpdating = true;
foreach (var item in viewModel.Items)
{
item.IsChecked = viewModel.SelectAllChecked;
}
viewModel._isUpdating = false;
}
}
}
}
Output
Download the complete sample from GitHub
Take a moment to explore this documentation, where you can find more information 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 implement select all checkbox column in SfDataGrid.
You can refer to our .NET MAUI DataGrid’s feature tour page to learn 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 on the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to explore our .NET MAUI DataGrid and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in the comments below. 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!