|
1 |
| -# How-to-implement-select-all-checkbox-column-in-.NET-MAUI-DataGrid-SfDataGrid |
2 |
| -This demo shows how to implement a Select All checkbox functionality within a checkbox column in the .NET MAUI DataGrid (SfDataGrid)? |
| 1 | +# How to implement select all checkbox column in .NET MAUI DataGrid(SfDataGrid) ? |
| 2 | +This sample demonstrates how to implement a **Select All** checkbox column in the Syncfusion [.NET MAUI DataGrid](https://help.syncfusion.com/maui/datagrid/overview) (`SfDataGrid`). It allows users to select or deselect all rows using a single checkbox in the header, while also enabling individual row selection. |
| 3 | + |
| 4 | +## Xaml |
| 5 | +``` |
| 6 | + <ContentPage.BindingContext> |
| 7 | + <local:SampleViewModel x:Name="viewModel" /> |
| 8 | + </ContentPage.BindingContext> |
| 9 | +
|
| 10 | + <syncfusion:SfDataGrid x:Name="SampleGrid" |
| 11 | + HeaderGridLinesVisibility="Both" |
| 12 | + GridLinesVisibility="Both" |
| 13 | + ItemsSource="{Binding Items}" |
| 14 | + ColumnWidthMode="Auto" |
| 15 | + AutoGenerateColumnsMode="None"> |
| 16 | + <syncfusion:SfDataGrid.Columns> |
| 17 | + <syncfusion:DataGridTemplateColumn HeaderText="Select All"> |
| 18 | + <syncfusion:DataGridTemplateColumn.HeaderTemplate> |
| 19 | + <DataTemplate> |
| 20 | + <buttons:SfCheckBox x:Name="checkBox" |
| 21 | + BindingContext="{Binding Source={x:Reference MyPage},Path=BindingContext}" |
| 22 | + IsThreeState="True" |
| 23 | + IsChecked="{Binding SelectAllChecked,Mode=TwoWay}" |
| 24 | + StateChanged="SfCheckBox_StateChanged_1" /> |
| 25 | + </DataTemplate> |
| 26 | + </syncfusion:DataGridTemplateColumn.HeaderTemplate> |
| 27 | + <syncfusion:DataGridTemplateColumn.CellTemplate> |
| 28 | + <DataTemplate> |
| 29 | + <buttons:SfCheckBox IsChecked="{Binding IsChecked}" StateChanged="SfCheckBox_StateChanged" /> |
| 30 | + </DataTemplate> |
| 31 | + </syncfusion:DataGridTemplateColumn.CellTemplate> |
| 32 | + </syncfusion:DataGridTemplateColumn> |
| 33 | + <syncfusion:DataGridCheckBoxColumn MappingName="SelectAllChecked" |
| 34 | + HeaderText="SelectAll" /> |
| 35 | + <syncfusion:DataGridTextColumn HeaderText="Name" |
| 36 | + MappingName="Name" /> |
| 37 | + <syncfusion:DataGridTextColumn HeaderText="Description" |
| 38 | + MappingName="Description" /> |
| 39 | + <syncfusion:DataGridTextColumn HeaderText="Category" |
| 40 | + MappingName="Category" /> |
| 41 | + <syncfusion:DataGridTextColumn HeaderText="Price" |
| 42 | + MappingName="Price" /> |
| 43 | + </syncfusion:SfDataGrid.Columns> |
| 44 | + </syncfusion:SfDataGrid> |
| 45 | +``` |
| 46 | + |
| 47 | + ## Xaml.cs |
| 48 | +``` |
| 49 | + public partial class MainPage : ContentPage |
| 50 | + { |
| 51 | + public MainPage() |
| 52 | + { |
| 53 | + InitializeComponent(); |
| 54 | + } |
| 55 | +
|
| 56 | + private void SfCheckBox_StateChanged(object sender, Syncfusion.Maui.Buttons.StateChangedEventArgs e) |
| 57 | + { |
| 58 | + var viewModel = BindingContext as SampleViewModel; |
| 59 | +
|
| 60 | + if (viewModel != null && !viewModel._isUpdating) |
| 61 | + { |
| 62 | + viewModel._isUpdating = true; |
| 63 | + if (viewModel.Items.All(x => x.IsChecked == true)) |
| 64 | + { |
| 65 | + viewModel.SelectAllChecked = true; |
| 66 | + } |
| 67 | + else if (viewModel.Items.All(x => x.IsChecked == false)) |
| 68 | + { |
| 69 | + viewModel.SelectAllChecked = false; |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + viewModel.SelectAllChecked = null; |
| 74 | + } |
| 75 | + viewModel._isUpdating = false; |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + private void SfCheckBox_StateChanged_1(object sender, StateChangedEventArgs e) |
| 80 | + { |
| 81 | + var viewModel = BindingContext as SampleViewModel; |
| 82 | + if (viewModel != null) |
| 83 | + { |
| 84 | + if (!viewModel._isUpdating) |
| 85 | + { |
| 86 | + viewModel._isUpdating = true; |
| 87 | + foreach (var item in viewModel.Items) |
| 88 | + { |
| 89 | + item.IsChecked = viewModel.SelectAllChecked; |
| 90 | + } |
| 91 | + viewModel._isUpdating = false; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +``` |
| 97 | + |
| 98 | +### ScreenShot |
| 99 | +<img src="https://support.syncfusion.com/kb/agent/attachment/inline?token=eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjQwNjY0Iiwib3JnaWQiOiIzIiwiaXNzIjoic3VwcG9ydC5zeW5jZnVzaW9uLmNvbSJ9._kkYJ5p5hPQUfYUfJ9Tbpuq3TZDI6S2xwqSCG5dzUKs" width=800/> |
| 100 | + |
| 101 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-implement-select-all-checkbox-column-in-.NET-MAUI-DataGrid-SfDataGrid) |
| 102 | + |
| 103 | + Take a moment to explore this [documentation](https://help.syncfusion.com/maui/datagrid/overview), where you can find more information about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples. Please refer to this [link](https://www.syncfusion.com/maui-controls/maui-datagrid) to learn about the essential features of Syncfusion .NET MAUI DataGrid (SfDataGrid). |
| 104 | + |
| 105 | +### Conclusion |
| 106 | +I hope you enjoyed learning about How to implement select all checkbox column in SfDataGrid. |
| 107 | + |
| 108 | +You can refer to our [.NET MAUI DataGrid’s feature tour](https://www.syncfusion.com/maui-controls/maui-datagrid) page to learn about its other groundbreaking feature representations. You can also explore our [.NET MAUI DataGrid Documentation](https://help.syncfusion.com/maui/datagrid/getting-started) to understand how to present and manipulate data. For current customers, you can check out our .NET MAUI components on the [License and Downloads](https://www.syncfusion.com/sales/teamlicense) page. If you are new to Syncfusion, you can try our 30-day [free trial](https://www.syncfusion.com/downloads/maui) to explore our .NET MAUI DataGrid and other .NET MAUI components. |
| 109 | + |
| 110 | +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](https://www.syncfusion.com/forums),[Direct-Trac](https://support.syncfusion.com/create) or [feedback portal](https://www.syncfusion.com/feedback/maui?control=sfdatagrid), or the feedback portal. We are always happy to assist you! |
0 commit comments