Completed
Last Updated: 06 Jun 2019 13:25 by ADMIN
Release R2 2019 SP1 (LIB 2019.2.610)
ADMIN
Dess | Tech Support Engineer, Sr.
Created on: 05 Dec 2016 08:19
Category: GridView
Type: Bug Report
1
FIX. RadGridView - incorrect child templates are expanded when clicking the expander
To reproduce:

Sub New()
     
    InitializeComponent()

    Dim dt1 As New DataTable
    dt1.Columns.Add("Id", GetType(Integer))
    dt1.Columns.Add("Name", GetType(String))
    For index = 1 To 20
        dt1.Rows.Add(index, "Parent" & index)
    Next
    Me.RadGridView1.MasterTemplate.DataSource = dt1
    Me.RadGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill

    Dim dt2 As New DataTable
    dt2.Columns.Add("Id", GetType(Integer))
    dt2.Columns.Add("Name", GetType(String))
    dt2.Columns.Add("ParentId", GetType(Integer))

    Dim dt3 As New DataTable
    dt3.Columns.Add("Id", GetType(Integer))
    dt3.Columns.Add("Name", GetType(String))
    dt3.Columns.Add("ParentId", GetType(Integer))
    For index = 1 To 20
        If index Mod 2 = 0 Then
            dt2.Rows.Add(index, "Child1." & index, index)
            If index Mod 4 = 0 Then
                dt3.Rows.Add(index, "Child2." & index, index)
            End If
        ElseIf index Mod 3 = 0 Then
            dt3.Rows.Add(index, "Child2." & index, index)
        Else
            dt3.Rows.Add(index, "Child2." & index, index)
        End If

    Next

    Dim template As New GridViewTemplate()
    template.DataSource = dt2
    template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
    RadGridView1.MasterTemplate.Templates.Add(template)

    Dim relation As New GridViewRelation(RadGridView1.MasterTemplate)
    relation.ChildTemplate = template
    relation.RelationName = "FirstChild"
    relation.ParentColumnNames.Add("Id")
    relation.ChildColumnNames.Add("ParentId")
    RadGridView1.Relations.Add(relation)

    Dim template2 As New GridViewTemplate()
    template2.DataSource = dt3
    template2.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
    RadGridView1.MasterTemplate.Templates.Add(template2)

    Dim relation2 As New GridViewRelation(RadGridView1.MasterTemplate)
    relation2.ChildTemplate = template2
    relation2.RelationName = "SecondChild"
    relation2.ParentColumnNames.Add("Id")
    relation2.ChildColumnNames.Add("ParentId")
    RadGridView1.Relations.Add(relation2)

    AddHandler Me.RadGridView1.ChildViewExpanding, AddressOf ChildViewExpanding
End Sub

Private Sub RadGridView1_ViewCellFormatting(sender As Object, e As Telerik.WinControls.UI.CellFormattingEventArgs) _
Handles RadGridView1.ViewCellFormatting
    Dim cell As GridDetailViewCellElement = TryCast(e.CellElement, GridDetailViewCellElement)
    Dim expanderCell As GridGroupExpanderCellElement = TryCast(e.CellElement, GridGroupExpanderCellElement)
    If expanderCell IsNot Nothing AndAlso TypeOf e.CellElement.RowElement Is GridDataRowElement Then
        Dim hierarchyRow As GridViewHierarchyRowInfo = DirectCast(expanderCell.RowInfo, GridViewHierarchyRowInfo)
        If Not IsExpandable(hierarchyRow) Then
            expanderCell.Expander.Visibility = Telerik.WinControls.ElementVisibility.Hidden
        Else
            expanderCell.Expander.Visibility = Telerik.WinControls.ElementVisibility.Visible
        End If
    ElseIf cell IsNot Nothing Then
        Dim hierarchyRow As GridViewHierarchyRowInfo = DirectCast(DirectCast(cell.RowInfo, GridViewDetailsRowInfo).Owner,  _
        GridViewHierarchyRowInfo)
        For i As Integer = 0 To cell.PageViewElement.Items.Count - 1
            Dim item As RadPageViewItem = cell.PageViewElement.Items(i)
            Dim viewInfo As GridViewInfo = hierarchyRow.Views(i)
            item.Text = "Child Template " & i
            If viewInfo.ChildRows.Count = 0 Then
                If i = 0 AndAlso i < cell.PageViewElement.Items.Count - 1 Then
                    cell.PageViewElement.Items(i + 1).IsSelected = True
                End If
                item.Visibility = Telerik.WinControls.ElementVisibility.Hidden
            Else
                item.Visibility = Telerik.WinControls.ElementVisibility.Visible
            End If
        Next
    End If
End Sub

Private Function IsExpandable(hierarchyRow As GridViewHierarchyRowInfo) As Boolean
    For Each view As GridViewInfo In hierarchyRow.Views
        If view.ChildRows.Count > 0 Then
            Return True
        End If
    Next
    Return False
End Function

Workaround:

AddHandler Me.RadGridView1.ChildViewExpanding, AddressOf RadGridView1_ChildViewExpanding 
AddHandler Me.RadGridView1.MouseDown, AddressOf RadGridView_MouseDown

Private Sub RadGridView1_ChildViewExpanding(sender As Object, e As ChildViewExpandingEventArgs)
    If lastClicked IsNot Nothing AndAlso e.ParentRow.Equals(lastClicked) Then
        e.Cancel = False
    Else
        e.Cancel = True
    End If

End Sub

Dim lastClicked As GridViewRowInfo

Private Sub RadGridView_MouseDown(sender As Object, e As MouseEventArgs)
    Dim expander As GridExpanderItem = TryCast(Me.RadGridView1.ElementTree.GetElementAtPoint(e.Location), GridExpanderItem)
    If expander IsNot Nothing Then
        lastClicked = DirectCast(expander.Parent, GridGroupExpanderCellElement).RowInfo 
    End If 
End Sub
0 comments