|
1 | | -# How-to-hide-a-column-on-button-click-using-MVVM-pattern-in-SfDataGrid |
2 | | -This demo shows how can I hide a column on button click using MVVM pattern in SfDataGrid? |
| 1 | +# How to hide a column on button click using MVVM pattern in SfDataGrid |
| 2 | +In this article, we will show you how to hide a column on button click using MVVM pattern in [.Net Maui DataGrid](https://www.syncfusion.com/maui-controls/maui-datagrid). |
| 3 | + |
| 4 | +## xaml |
| 5 | +``` |
| 6 | + <ContentPage.BindingContext> |
| 7 | + <local:EmployeeViewModel x:Name="viewModel" /> |
| 8 | +</ContentPage.BindingContext> |
| 9 | +
|
| 10 | +<StackLayout> |
| 11 | + <Button Text="Hide Employee Name Column" |
| 12 | + WidthRequest="230" Command="{Binding HideColumnCommand}" CommandParameter="{x:Reference dataGrid}" |
| 13 | + HeightRequest="55" /> |
| 14 | + <syncfusion:SfDataGrid x:Name="dataGrid" |
| 15 | + Margin="10" |
| 16 | + GridLinesVisibility="Both" |
| 17 | + HeaderGridLinesVisibility="Both" |
| 18 | + HorizontalOptions="FillAndExpand" |
| 19 | + VerticalOptions="FillAndExpand" |
| 20 | + ColumnWidthMode="Auto" |
| 21 | + AutoGenerateColumnsMode="None" |
| 22 | + ItemsSource="{Binding Employees}"> |
| 23 | +
|
| 24 | + <syncfusion:SfDataGrid.Columns> |
| 25 | + <syncfusion:DataGridNumericColumn MappingName="EmployeeID" |
| 26 | + Format="#" |
| 27 | + HeaderText="Employee ID" /> |
| 28 | + <syncfusion:DataGridTextColumn MappingName="Name" |
| 29 | + HeaderText="Employee Name" /> |
| 30 | + <syncfusion:DataGridTextColumn MappingName="Title" |
| 31 | + HeaderText="Designation" /> |
| 32 | + <syncfusion:DataGridDateColumn MappingName="HireDate" |
| 33 | + HeaderText="Hire Date" /> |
| 34 | +
|
| 35 | + </syncfusion:SfDataGrid.Columns> |
| 36 | +
|
| 37 | + </syncfusion:SfDataGrid> |
| 38 | +</StackLayout> |
| 39 | +``` |
| 40 | + |
| 41 | +## C# |
| 42 | +The code below demonstrates how to hide a column on button click using MVVM pattern in DataGrid. |
| 43 | +**EmployeeViewModel.cs** |
| 44 | +``` |
| 45 | +public ICommand HideColumnCommand { get; } |
| 46 | +
|
| 47 | +public EmployeeViewModel() |
| 48 | +{ |
| 49 | + PopulateData(); |
| 50 | + this.CustomerNames = Customers.ToObservableCollection(); |
| 51 | + HideColumnCommand = new Command<object>(ExecuteHideColumn); |
| 52 | + employees = this.GetEmployeeDetails(50); |
| 53 | +} |
| 54 | +
|
| 55 | +private void ExecuteHideColumn(object parameter) |
| 56 | +{ |
| 57 | + var dataGrid = parameter as SfDataGrid; |
| 58 | + if (dataGrid != null) |
| 59 | + { |
| 60 | + var columnName = dataGrid.Columns.FirstOrDefault(c => c.MappingName == "Name"); |
| 61 | + if (columnName != null) |
| 62 | + { |
| 63 | + columnName.Visible = false; |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +  |
| 70 | + |
| 71 | +[View sample in GitHub](https://github.com/SyncfusionExamples/How-to-hide-a-column-on-button-click-using-MVVM-pattern-in-SfDataGrid) |
| 72 | + |
| 73 | +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). |
| 74 | + |
| 75 | +##### Conclusion |
| 76 | + |
| 77 | +I hope you enjoyed learning about how to hide a column on button click using MVVM pattern in .NET MAUI DataGrid (SfDataGrid). |
| 78 | + |
| 79 | +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. |
| 80 | +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. |
| 81 | + |
| 82 | +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