Delete column/hide column from ViewModel

hi,

how to delete/hide column from viewmodel?

1 Reply 1 reply marked as answer

KK Karthikraja Kalaimani Syncfusion Team May 21, 2021 06:53 AM UTC

Hi Rahul, 

You requirement can be achieved by binding a viewmodel property to IsHidden Property of GridColumns. For more details please refer to the below code snippet. 

Code snippet : 
 <sfgrid:SfDataGrid x:Name="sfGrid"  NavigationMode="Cell"
                                               SelectionMode="Single"
                                               AllowSorting="True"
                                               ColumnSizer="Auto"
                                               AllowEditing="True"
                                               AllowKeyboardNavigation="True"
                                               AutoGenerateColumns="True"
                                               AllowResizingColumn="True"
                                               ItemsSource="{Binding OrdersInfo}" >
            <sfgrid:SfDataGrid.Columns>
                <sfgrid:GridTextColumn IsHidden="{Binding CanShow}" Width="300" MappingName="OrderID" />
                <sfgrid:GridNumericColumn Width="200" MappingName="OrderID"></sfgrid:GridNumericColumn>
                <sfgrid:GridComboBoxColumn Width="100" BindingContext="{x:Reference viewModel}"
                                 HeaderText="Dealer Name"                                
                                 ItemsSource="{Binding CustomerNames}"                                
                                 MappingName="OrderID"></sfgrid:GridComboBoxColumn>
                <sfgrid:GridDateTimeColumn Width="100" MappingName="ShippedDate"></sfgrid:GridDateTimeColumn>
                <sfgrid:GridPickerColumn Width="100" MappingName="OrderID" BindingContext="{x:Reference viewModel}" ItemsSource="{Binding CustomerNames}"></sfgrid:GridPickerColumn>
            </sfgrid:SfDataGrid.Columns>
            <sfgrid:SfDataGrid.UnboundRows>
                <sfgrid:GridUnboundRow Position="Bottom"></sfgrid:GridUnboundRow>
            </sfgrid:SfDataGrid.UnboundRows>
        </sfgrid:SfDataGrid>

//ViewModel.cs 
 public class ViewModel : INotifyPropertyChanged
    {
...
  private bool canshow = true;
        public bool CanShow
        {
            get
            {
                return canshow;
            }
            set
            {
                canshow = value;
                RaisePropertyChanged("CanShow");
            }
        }
...
     }
Regards,
Karthik Raja


Marked as answer
Loader.
Up arrow icon