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

Hide Collapse image when no records using an object to populate detail table

4 Answers 133 Views
Grid
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 20 May 2011, 03:30 AM
I tried the http://www.telerik.com/help/aspnet-ajax/grid-hide-expand-collapse-images-when-no-records.html way of hiding the expand/collapse image but with no success. I don't use the HierarchyLoadMode = ServerBind way of populating the details table - but I don't have a problem populating the details table.

Here is the code for the detail table bind and my attempt to hide the collapse/expand column for parent rows with no detail items:

Private Sub RadGridEvents_DetailTableDataBind(ByVal source As Object, ByVal e As GridDetailTableDataBindEventArgs) Handles RadGridEvents.DetailTableDataBind
        Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
        Select Case e.DetailTableView.Name
            Case "EventMerchantList"
                Dim EventID As Integer = dataItem.GetDataKeyValue("eventID")
                Dim eventML As List(Of Merchant)
                eventML = Merchant.GetAllMerchantsByEventID(EventID)
                If Not eventML Is Nothing Then
                    If eventML.Count > 0 Then
                        e.DetailTableView.DataSource = eventML
                    End If
                End If
        End Select
    End Sub
 
    Protected Sub RadGridEvents_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        HideExpandColumnRecursive(RadGridEvents.MasterTableView)
    End Sub
    Public Sub HideExpandColumnRecursive(ByVal tableView As GridTableView)
        Dim nestedViewItems As GridItem() = tableView.GetItems(GridItemType.NestedView)
        For Each nestedViewItem As GridNestedViewItem In nestedViewItems
            For Each nestedView As GridTableView In nestedViewItem.NestedTableViews
                If nestedView.Items.Count = 0 Then
                    Dim cell As TableCell = nestedView.ParentItem("ExpandColumn")
                    cell.Controls(0).Visible = False
                    nestedViewItem.Visible = False
                End If
                If nestedView.HasDetailTables Then
                    HideExpandColumnRecursive(nestedView)
                End If
            Next
        Next
    End Sub

It seems like this should be an easy thing to do but I cannot find the answer. Thanks in advance for any help I get with this.

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 20 May 2011, 06:26 AM
Hello William,

I tried the same scenario and it worked as expected when I set HierarchyLoadMode to Client/ServerBind. The HierarchyLoadMode is "ServerOnDemand" by default. In this case, the roundtrip to the database happens when the grid is bound and when an item is expanded. Only detail table-views of the expanded items are rendered.So I suggest you to set HierarchyLoadMode to Client/ServerBind. Check the following documentation which explains more about this.
Hierarchy load modes.

Thanks,
Princy.



0
William
Top achievements
Rank 1
answered on 20 May 2011, 01:03 PM
Thank you for your reply. I need to see how you set up the Grids on the front end because when I change the properties to HierarchyLoadMode.Client or to ServerBind, I get an error saying that the DAL.EventList' does not contain a property with the name 'MerchantID which is true as they are different tables and belong to different classes.

The grid works fine using the default ServerOnDemand - just can't get rid of the collapse/expand arrows when there are no child results.

Is this only for a self-referencing scenario? I am using stored procedures with joins to get the merchant class that I use to populate the child table. It is a many to many inner join as the eventlist table doesn't have a MerchantID and the Merchant table doesn't have a eventID.

Thanks again for your help.
0
William
Top achievements
Rank 1
answered on 25 May 2011, 12:30 AM
Well, after spending a couple of days on this, finally figured out a solution. It is not perfect because it requires to see if child records are present by calling an object for the child nodes which defeats the purpose of loading the child nodes on demand. However, the overhead is pretty small and the end result of hiding the collapse/expand cell when there are no child nodes - or details in the detail table - is well worth it.

Here is the code that I used:

Private Sub RadGridEvents_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGridEvents.ItemDataBound
        If e.Item.OwnerTableView.Name = "MasterTable" Then
            If TypeOf (e.Item) Is GridDataItem Then
                Dim dataBoundItem As GridDataItem = DirectCast(e.Item, GridDataItem)
                Dim EventID As Integer = CInt(dataBoundItem.GetDataKeyValue("eventID").ToString())
                Dim catcheck As List(Of EventMerchant)
                catcheck = EventMerchant.GetAllEventMerchantsByEventID(EventID)
                If Not catcheck Is Nothing Then
                    If catcheck.Count < 1 Then
                        Dim cell As TableCell = dataBoundItem.Item("ExpandColumn")
                        cell.Controls(0).Visible = False
                    End If
                Else
                    Dim cell As TableCell = dataBoundItem.Item("ExpandColumn")
                    cell.Controls(0).Visible = False
                End If
            End If
        End If
    End Sub

First, I had to name the Master Table as "MasterTable" - not so creative, I know - but of course any name will do. Then an "IF" statement was necessary to see if the item was a part of the MasterTable and then an "IF" statement to see if the item is a GridDataItem. Next you need the primary key of the master table GridDataItem to call the list of EventMerchant (i.e. detail table items) objects. If the list is Nothing or if the list has a count less than zero - seems weird but you better do both logic tests - then the ExpandColumn contents are not visible. The main deal of this whole thing is naming the master table and then using the Itembound sub to perform the logic tests on ONLY the MasterTable items as Itembound is used for BOTH the master and detail tables. I used objects but doing it the datasets/dataadapters/datatables methods will work as well.

Might be good in the future if Telerik included some many-to-many Grid examples.
0
Marin
Telerik team
answered on 26 May 2011, 07:57 AM
Hello William,

Thank you for sharing your approach with the community.

Indeed in both approaches you need to know the child items of the detail table so that you can determine whether to hide or not the expand collapse images. It also depends on the configuration of the hierarchy where sometimes the approach from the help topic for traversing the items in Page_PreRender (even with default HierarchyLoadMode) might also be used.

Thank you,
Marin
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
William
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
William
Top achievements
Rank 1
Marin
Telerik team
Share this question
or