Child Grid Column Visibility Based On Value of Field in Parent Row

4 posts, 0 answers
  1. iCeMAn
    iCeMAn avatar
    5 posts
    Member since:
    Nov 2010

    Posted 24 Nov 2010 Link to this post

    Hi guys,

    This has been eluding me for some time so I have decided to pose my scenario to the forum. I have two objects as follows:

    Public Class A
        Public Property FieldA1 As String   
        Public Property FieldA2 As String
        Public Property FieldA3 As IEnumerable(Of B)
    End Class
     
    Public Class B
        Public Property FieldB1 As String
        Public Property FieldB2 As String
        Public Property FieldB3 As String
    End Class

    I have two grids declared as follows:

    <telerik:RadGridView x:Name="GridA"
                         AutoGenerateColumns="False"
                         RowIndicatorVisibility="Collapsed"
                         RowDetailsVisibilityMode="VisibleWhenSelected">
     
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding FieldA1}" Header="Field A1"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding FieldA2}" Header="Field A2"/>
        </telerik:RadGridView.Columns>
         
        <telerik:RadGridView.RowDetailsTemplate>
            <DataTemplate>
                        <telerik:RadGridView x:Name="GridB" ItemsSource="{Binding FieldA3}"
                                             AutoGenerateColumns="False"
                                             RowIndicatorVisibility="Visible">
                            <telerik:RadGridView.Columns>
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding FieldB1}" Header="Field B1"/>
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding FieldB2}" Header="Field B2"/>
                                <telerik:GridViewDataColumn DataMemberBinding="{Binding FieldB3}" Header="Field B3" Visibility={Binding Converter={StaticResource VisibilityConverter}}/>
                            </telerik:RadGridView.Columns>
                        </telerik:RadGridView>
            </DataTemplate>
        </telerik:RadGridView.RowDetailsTemplate>
    </telerik:RadGridView>

    My converter is as follows:
    Imports System.Windows.Data
     
    Namespace Converters
        Public Class VisibilityConverter
            Implements IValueConverter
     
            Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
                Dim _convertedValue As Visibility
     
                                     'code not implemented yet

                Return _convertedValue
            End Function
     
            Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
                'not implemented since this is a one-way binding
                Throw New NotImplementedException
            End Function
        End Class
    End Namespace

    The problem is that the first parameter (value as Object) is of type ClassB and not ClassA. Because of this I am unable to set the visibility of the FieldB3 column in the child grid based on a field/column value in the parent row (ClassA). Can someone point me in the right direction? 

    Thanks in advance (and apologies for the wordy post).
  2. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 24 Nov 2010 Link to this post

    Hi,

    You can easily do this in code behind - subscribe to the RowDetailsVisibilityChanged event and get the parent row and the child grid from its event arguments. Here is a sample code:


    If e.Visibility.HasValue AndAlso e.Visibility.Value = Visibility.Visible Then
        Dim childGrid As RadGridView = TryCast(e.DetailsElement, RadGridView)
            'This is the parent row
        Dim c As Club = TryCast(e.Row.Item, Club)
      
        If c.StadiumCapacity < 50000 Then
            childGrid.Columns("Name").IsVisible = False
        End If
    End If

    Note that the column has a property IsVisible, not Visibility.



    Sincerely yours,
    Veselin Vasilev
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for Silverlight
  3. DevCraft banner
  4. iCeMAn
    iCeMAn avatar
    5 posts
    Member since:
    Nov 2010

    Posted 24 Nov 2010 Link to this post

    Ah! Works like a charm! I thought it could have been achieved declaratively. Thanks for the quick response.
  5. Hemanth
    Hemanth avatar
    24 posts
    Member since:
    Mar 2011

    Posted 20 Nov 2012 Link to this post

    Hi can Some one give me this code in C#, i m trying a lot but its not working for me

    Thanks in Advance
Back to Top