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

Nested Grid with differing number of columns

5 Answers 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Katie
Top achievements
Rank 1
Katie asked on 02 Oct 2009, 09:46 PM
I have a scenerio that I am trying to find a solution to. 
See the attached pictures.  One shows before, the other shows what I want to acheive.

I have a 2 level nested grid.  Level 1 shows a list of products (Polo Shirt, Fleece, Mug...).  Level two shows the options that each product is available. Depending on the product, it may have a different number of features.  Ex. Shirt will have color, size, and style where as the mug will only have color.  Data rows for the second table return 10 columns label Feature1 .. Feature10. 

I want to be able to hide the columns that do not apply to that product and to rename the column to the appropriate name. Ex. Polo shirt should show columns 1-3 and hide columns 4-10 and the names of columns 1-3 should be changed color, size, style (instead of Feature1, Feature2, Feature3).  And for the Mug only column 1  should show while columns 2 - 10 are hidden. The name of the column should be changed to color. The names of the columns are available in the rows returned for the 2nd level grid.


Any help would be much appreciated.

5 Answers, 1 is accepted

Sort by
0
Accepted
Yavor
Telerik team
answered on 07 Oct 2009, 10:53 AM
Hello Jeff,

One possible option in this case would be to iterate through the control's structure at runtime, and hide the columns accordingly. Additional information on how to iterate through the elements in the hierarchy is available in the following article:

http://www.telerik.com/help/aspnet-ajax/grdtraversingdetailtablesitemsingrid.html

I hope this information helps.

Sincerely yours,
Yavor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Katie
Top achievements
Rank 1
answered on 07 Oct 2009, 03:51 PM

Thanks, that definitely helped.  I was able to make the appropriate columns visible or invisible, but the column headers are not changing.

Here is my code. 

Where I set column.visible = true  I am also setting the column.headertext to the appropriate value. I used the debugger and the correct value is being set,  but the original header text is still showing.

    Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadGrid1.PreRender  
 
        ' Loop through each dataitem in the MasterTableView  
        For Each item As GridDataItem In RadGrid1.MasterTableView.Items  
            ' Get the first Nested Table View  
            Dim nestedTableView As GridTableView  
            nestedTableView = item.ChildItem.NestedTableViews(0)  
 
            ' Process only if NestedTableView is expanded or databound  
            If nestedTableView.Items.Count > 0 Then 
                Dim dt As DataTable  
                Dim FeatureCount As Integer 
                Dim column As GridColumn  
 
                FeatureCount = 0  
                ' Get Product Info based on first record in nested table  
                dt = GetProductInfo(nestedTableView.Items(0).GetDataKeyValue("ProductID").ToString)  
                ' Set Feature count  
                FeatureCount = dt.Rows(0).Item("FeatureCount")  
 
                ' Loop through all ten features and make visible or invisible as necessary  
                For i As Integer = 1 To 10  
                    column = nestedTableView.GetColumnSafe("FeatureDescription" & i)  
                    If i > FeatureCount Then 
                        column.Visible = False 
                    Else 
                        column.Visible = True 
                        column.HeaderText = dt.Rows(0).Item("FeatureLabel" & i).ToString  
                    End If 
                Next 
            End If 
        Next 
 
    End Sub 
 
    Protected Function GetProductInfo(ByVal ProductID As StringAs DataTable  
        Dim cn As New SqlConnection(ConfigurationManager.ConnectionStrings("SGHPUBSConnectionString").ConnectionString)  
        Dim cmd As New SqlCommand  
        cmd.Connection = cn  
        cmd.CommandType = CommandType.StoredProcedure  
        cmd.CommandText = "GetProductsByProductID" 
        cmd.Parameters.Add(New SqlParameter("GetProductID", ProductID))  
        Dim da As New SqlDataAdapter(cmd)  
        Dim dt As New DataTable  
        Try 
            da.Fill(dt)  
        Catch ex As Exception  
 
        End Try 
        cn.Close()  
 
        Return dt  
    End Function 
 
0
Katie
Top achievements
Rank 1
answered on 07 Oct 2009, 05:53 PM
I managed to get the header text to change by using the GridHeaderItem, but for some reason using this method is disabling sorting. I don't understand why changing the header text prevents sorting, but any insight would be appreciated.

Here is my modified code.
    Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadGrid1.PreRender  
 
        ' Loop through each dataitem in the MasterTableView  
        For Each item As GridDataItem In RadGrid1.MasterTableView.Items  
            ' Get the first Nested Table View  
            Dim nestedTableView As GridTableView  
            nestedTableView = item.ChildItem.NestedTableViews(0)  
 
            ' Process only if NestedTableView is expanded and databound  
            If nestedTableView.Items.Count > 0 And item.Expanded Then 
                Dim dt As DataTable  
                Dim FeatureCount As Integer 
                Dim column As GridColumn  
                Dim headerItem As GridHeaderItem  
 
                FeatureCount = 0  
                ' Get Product Info based on first record in nested table  
                dt = GetProductInfo(nestedTableView.Items(0).GetDataKeyValue("ProductID").ToString)  
                ' Set Feature count  
                FeatureCount = dt.Rows(0).Item("FeatureCount")  
                ' Get Header Item for Nested Table  
                headerItem = nestedTableView.GetItems(GridItemType.Header)(0)  
 
                ' Loop through all ten features and make visible or invisible as necessary  
                For i As Integer = 1 To 10  
                    column = nestedTableView.GetColumnSafe("FeatureDescription" & i)  
                    If i > FeatureCount Then 
                        column.Visible = False 
                    Else 
                        column.Visible = True 
                        'column.HeaderText = dt.Rows(0).Item("FeatureLabel" & i).ToString  
                        headerItem("FeatureDescription" & i).Text = dt.Rows(0).Item("FeatureLabel" & i).ToString  
                    End If 
                Next 
            End If 
        Next 
 
    End Sub 
0
Accepted
Princy
Top achievements
Rank 2
answered on 08 Oct 2009, 07:30 AM
Hi Jeff,

When you have sorting enabled in your grid, the header cell would contain a linkbutton. So, you would have to access the linkbutton in the header and change its text. Hence, try replacing your code with the following code to enable sorting, when you want to dynamically change your header text:
vb:
Protected Sub RadGrid1_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadGrid1.PreRender    
   
        ' Loop through each dataitem in the MasterTableView    
        .... 
   
            ' Process only if NestedTableView is expanded and databound    
             .... 
                ' Get Product Info based on first record in nested table    
                 .....   
                ' Get Header Item for Nested Table    
                 .... 
                ' Loop through all ten features and make visible or invisible as necessary    
                  ... 
                    If i > FeatureCount Then   
                        column.Visible = False   
                    Else   
                        column.Visible = True   
                        (DirectCast(headerItem("FeatureDescription" & i).Controls(0), LinkButton)).Text = dt.Rows(0).Item("FeatureLabel" & i).ToString 'replaced line  
                         
                    End If   
                Next   
            End If   
        Next   
    End Sub  

Hope this helps..
Princy.
0
Katie
Top achievements
Rank 1
answered on 08 Oct 2009, 05:18 PM
Thanks Princy, that worked.
Tags
Grid
Asked by
Katie
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Katie
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or