none
Hide/Show Template Column in Datagrid in code

    Question

  • Hi I'm trying to display (or hide) a template column in the datagrid that contains a button. I want to show or hide the column based the IsEnabled property of another button on the page (not in the datagrid.)

    I can get it to work by refering to the column number (column(0) or column(1)) on the LoadedRows property of the datagrid. Is there a way to refer to the column without using column#?

    Code that works:

    Private Sub dgrdTransactions_LoadingRow(sender As Object, e As System.Windows.Controls.DataGridRowEventArgs) Handles dgrdTransactions.LoadingRow
            If btnUploadMobil.IsEnabled Then
                Dim grdColumn = dgrdTransactions.Columns(0)
                If grdColumn IsNot Nothing Then
                    grdColumn.Visibility = Windows.Visibility.Visible
                End If
                Dim grdColumn1 = dgrdTransactions.Columns(1)
                If grdColumn1 IsNot Nothing Then
                    grdColumn1.Visibility = Windows.Visibility.Visible
                End If
            End If
    End Sub
    

    Or is there a way to bind the visibility of the column to the IsEnabled property of the button?

    Friday, March 16, 2012 12:38 PM

Answers

  • I just did a little test case and realized that the DataGrid.Column Visibility property doesn't support binding type even in Silverlight 5. So it looks like, for now, you're better off with setting the datagrid column visibility in code. But it'll be a hybrid-MVVM. In practical, there're probably many hybrid similar like this situation.

    Tuesday, March 20, 2012 1:31 PM
  • Try this:

    DataGridColumn column1 = datagrid.Columns.Where(c=>(string)c.Header == "Column 1").FirstOrDefault();
    column1.Visibility = System.Windows.Visibility.Collapsed;

    Tuesday, March 20, 2012 3:54 PM
  • I came up with this solution to enable visiblity binding on a data grid column:

        public class ExtendedDataGridColumn : DataGridTemplateColumn
        {
            public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register("Visibility", typeof(Visibility), typeof(DataGridTemplateColumn), new PropertyMetadata(Visibility.Visible, VisibilityChanged));
            public new Visibility Visibility
            {
                get { return (Visibility)GetValue(VisibilityProperty); }
                set { SetValue(VisibilityProperty, value); }
            }
            private static void VisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if ((DataGridTemplateColumn)d != null)
                {
                    ((DataGridTemplateColumn)d).Visibility = (Visibility)e.NewValue;
                }
            }
        }

    Data grid columns do not support ElementName binding, so you have to handle it through a view model and the view model needs to be defined as a static resource. Usage is:

    <myCtrls:ExtendedDataGridColumn Header="First Name" Visibility="{Binding FirstNameVisibility, Source={StaticResource ViewModel}}" SortMemberPath="FirstName">
       <myCtrls:ExtendedDataGridColumn.CellTemplate>
          <DataTemplate>
             <TextBlock Margin="5,0" Text="{Binding FirstName}" />
          </DataTemplate>
       </myCtrls:ExtendedDataGridColumn.CellTemplate>
    </myCtrls:ExtendedDataGridColumn>

    Thursday, March 22, 2012 8:52 AM

All replies

  • You can bind the datagrid column visibility property to the button IsEnabled but since they're of different type (bool versus Windows.Visibility) you'll need to provide a converter which is an implement of IValueConverter.

    Friday, March 16, 2012 6:27 PM
  • can you direct me to a good resource. I tried it different ways but couldn't get it to work.

    Friday, March 16, 2012 6:36 PM
  • Hi Gopre400,

    To achieve the goal, you can utilize one of the new features of Silverlight 5, Binding on Style Setter. As G20 suggested, "bind the datagrid column visibility property to the button IsEnabled".

    You may take a look at a test sample I posted in below thread:

    http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/f5cb8e0e-17d9-4a53-974c-b66ed752b23f//1/10?Re+Binding+Visibility+of+StackPanel+in+a+ListBox+ItemTemplate

    You can use this page to convert C# to VB.NET.

    Best Regards,

    Monday, March 19, 2012 3:41 AM
  • As far as I know, data grid column visiblity does not support binding, see here:

    http://stackoverflow.com/questions/3433396/why-can-i-not-bind-the-visiblity-of-a-datagridtemplatecolumn-in-silverlight-4

    This would be a nice feature, please correct me if I am wrong.

     

    Monday, March 19, 2012 8:24 AM
  • I'm not using silverlight 5

    Monday, March 19, 2012 11:46 AM
  • That link explains how to bind the data grid column content visiblity, not the data grid column visibilty.

    Tuesday, March 20, 2012 8:35 AM
  • It also discusses binding the visiblity based on data in a class, and not based on the property of another element.

    Tuesday, March 20, 2012 11:25 AM
  • I just did a little test case and realized that the DataGrid.Column Visibility property doesn't support binding type even in Silverlight 5. So it looks like, for now, you're better off with setting the datagrid column visibility in code. But it'll be a hybrid-MVVM. In practical, there're probably many hybrid similar like this situation.

    Tuesday, March 20, 2012 1:31 PM
  • Is there a away tp refer to the column name and not column number?  NOT (column(0) or column(1))

    Tuesday, March 20, 2012 2:35 PM
  • Try this:

    DataGridColumn column1 = datagrid.Columns.Where(c=>(string)c.Header == "Column 1").FirstOrDefault();
    column1.Visibility = System.Windows.Visibility.Collapsed;

    Tuesday, March 20, 2012 3:54 PM
  • I came up with this solution to enable visiblity binding on a data grid column:

        public class ExtendedDataGridColumn : DataGridTemplateColumn
        {
            public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register("Visibility", typeof(Visibility), typeof(DataGridTemplateColumn), new PropertyMetadata(Visibility.Visible, VisibilityChanged));
            public new Visibility Visibility
            {
                get { return (Visibility)GetValue(VisibilityProperty); }
                set { SetValue(VisibilityProperty, value); }
            }
            private static void VisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                if ((DataGridTemplateColumn)d != null)
                {
                    ((DataGridTemplateColumn)d).Visibility = (Visibility)e.NewValue;
                }
            }
        }

    Data grid columns do not support ElementName binding, so you have to handle it through a view model and the view model needs to be defined as a static resource. Usage is:

    <myCtrls:ExtendedDataGridColumn Header="First Name" Visibility="{Binding FirstNameVisibility, Source={StaticResource ViewModel}}" SortMemberPath="FirstName">
       <myCtrls:ExtendedDataGridColumn.CellTemplate>
          <DataTemplate>
             <TextBlock Margin="5,0" Text="{Binding FirstName}" />
          </DataTemplate>
       </myCtrls:ExtendedDataGridColumn.CellTemplate>
    </myCtrls:ExtendedDataGridColumn>

    Thursday, March 22, 2012 8:52 AM
  • Thanks I'll test it!

    Friday, March 23, 2012 9:50 AM