This is a migrated thread and some comments may be shown as answers.

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

3 Answers 143 Views
GridView
This is a migrated thread and some comments may be shown as answers.
iCeMAn
Top achievements
Rank 1
iCeMAn asked on 24 Nov 2010, 03:54 PM
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).

3 Answers, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 24 Nov 2010, 04:31 PM
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
0
iCeMAn
Top achievements
Rank 1
answered on 24 Nov 2010, 05:21 PM
Ah! Works like a charm! I thought it could have been achieved declaratively. Thanks for the quick response.
0
Hemanth
Top achievements
Rank 1
answered on 21 Nov 2012, 04:15 AM
Hi can Some one give me this code in C#, i m trying a lot but its not working for me

Thanks in Advance
Tags
GridView
Asked by
iCeMAn
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
iCeMAn
Top achievements
Rank 1
Hemanth
Top achievements
Rank 1
Share this question
or