Hide/UnHide column dynamically

Hello,

I try to hide or unhide a column of a SfDataGrid by the help of a button.
When the datagrid is launched the column, for which the property IsHidden is bind to my ViewModel, is correctly hidden.
The issue appears when I click on the button that changes the property IsHidden of my ViewModel, the column is not hidden or visible.

I reproduced the issue based on your sample attached to this post.

Thanks in advance for your help.



Attachment: testSfDatagrid_e28d4800.zip

3 Replies

DY Deivaselvan Y Syncfusion Team August 10, 2018 11:03 AM UTC

Hi Sebastian,

You have missed to set source for the command which you assigned for the button in your sample, causes the issue. Can you please do the below change in your application to meet your requirement for hide/unhide the column dynamically.

 
<Button Content="Hide" Command="{Binding HiddenCommand, Source={StaticResource viewModel}}"/> 

Please let us know if you have any other questions.

Regards,
Deivaselvan 



SE Sebastien August 10, 2018 02:23 PM UTC

Dear Deivaselvan,

Thanks for your quick answer/help.

I tried what you suggested and it works in this small sample. 
Could you explain me why the source must be specified in the command binding? 

Unfortunately, I am facing issue when I transpose this sample in our project, could you help me a little bit more ? 
I attached a second sample to this reply. 

Kind regards, 

Sebastian

Attachment: testSfDatagrid_31cc2035.zip


DY Deivaselvan Y Syncfusion Team August 30, 2018 10:01 AM UTC

Hi Sebastien, 

Thank you for the update.

We have tried to achieve your requirement “To Hide/Unhide the column” in xaml when DataGrid control added into the ListItemTemplate, but framework wise it doesn’t work to bind the current source to the IsHidden property  due to it was bound with the two separate ViewModel to populate data to the each DataGrid control in the list view. So, we have achieved your requirement “To Hide/Unhide the column” in Loaded event of SfDataGrid control. Please refer the code example from below.  
Code snippets.  
XAML  
<DataTemplate>  
    <StackPanel>  
        <Button Content="Hide" Command="{Binding DataContext.HiddenCommand,ElementName=dataGrid}"/>  
  
        <syncfusion:SfDataGrid x:Name="dataGrid"    
                                ItemsSource="{Binding Orders}"   
                                AutoGenerateColumns="False" DataContext="{Binding Orders}">  
            <interactivity:Interaction.Behaviors>  
                <local:SfDataGridBehavior/>   
            </interactivity:Interaction.Behaviors>  
            <syncfusion:SfDataGrid.Columns>  
                <syncfusion:GridTextColumn MappingName="CustomerID"/>  
                <syncfusion:GridTextColumn MappingName="CustomerName"/>  
                <syncfusion:GridTextColumn MappingName="Country" />  
            </syncfusion:SfDataGrid.Columns>  
        </syncfusion:SfDataGrid>  
    </StackPanel>  
</DataTemplate>  
  
C#  
private void DataGrid_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)  
 {  
            
            object data = dataGrid.DataContext as ViewModel;  
            var column = dataGrid.Columns.FirstOrDefault(x => x.MappingName =="Country");  
            Windows.UI.Xaml.Data.Binding myBinding = new Windows.UI.Xaml.Data.Binding();  
            myBinding.Source = data;  
            myBinding.Path = new PropertyPath("IsHidden");  
            myBinding.Mode = BindingMode.TwoWay;  
            myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;  
            BindingOperations.SetBinding(column, GridColumnBase.IsHiddenProperty, myBinding);  
 }  
 
Please find the sample for the same from below location. Try running this sample and let us know if this helps you. 


Loader.
Up arrow icon