Filtering Related RadGriViews

1 Answer 26 Views
GridView
João
Top achievements
Rank 1
João asked on 11 Jan 2022, 07:43 PM | edited on 12 Jan 2022, 06:22 PM

Hello,

I am developing a form that has 2 related RadGrids.
Everything works fine except for the filter in the Child template.
Since its two grids related through a column, each parent line ends up with a "sub grid" of the second one. However, that is just the second grid "filtered" as if it was multiple. Am I correct?

Problem is, when I filter one of those Childs, the filter is applied in all of them like the image below.
I want to filter just that specific Child. Which in this case, is the first one.

Is that possible? How?

Another question I have is about the way this relation is built.

Lets say that the last row "DIVERSOS" has no child rows. I know I can remove the "+" icon but is there a way make that row not appear at all? In this case, if I have no child rows, I don't want to see the parent row either
I can make it so my Datasource doesnt have it. I tried to build it backwards. Each parent row is only added to my datatable if it has childs, but I want to have access to everything in my datasources.

I am using the Telerik 2021.2.511.40. if that helps.

Thanks in advance.

1 Answer, 1 is accepted

Sort by
0
Dinko
Telerik team
answered on 14 Jan 2022, 10:03 AM

Hello João,

Thank you for the provided details.

Looking at the image I can't be sure why the filter row value is applied to the other child rows. I will need to take a look at your implementation and debug the code so that I can narrow down the reason. Could it be possible to isolate this behavior in a standalone project so that I can have a better look at your scenario and propose a suitable solution?

Regarding your second question, you can subscribe to the CreateRow event of the GridViewElement. Inside the event handler, you can check if the current RowInfo property from the event arguments is GridViewHierarchyRowInfo. If yes, check the ChildRows collection if it has items inside. If not, set the IsVisible property to false.

this.radGridView1.GridViewElement.CreateRow += GridViewElement_CreateRow;

private void GridViewElement_CreateRow(object sender, GridViewCreateRowEventArgs e)
{
    if (e.RowInfo is GridViewHierarchyRowInfo)
    {
        var info = e.RowInfo;
        if(info.ChildRows.Count == 0 && info.Parent is MasterGridViewTemplate)
        {
            info.IsVisible = false;
        }
    }            
}

Regards,
Dinko
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

João
Top achievements
Rank 1
commented on 14 Jan 2022, 09:44 PM | edited

Thank you for your answer.

I attached a small and simple project to replicate the filtering issue. I couldnt add my Telerik dlls to the zip because the size was bigger than the allowed.
I have these records.

Filtering 1 child applies it to all others.

I also added your solution to set a parent row invisible if it has no childs but it doesnt seem to work.
In this example, I have a parent row with the key "Record3" but there are no childs with that key and the parent row still appears.
Am I doing something wrong? I tried to subscribe the event before populating my grid but by doing that, no rows appear at all.




Thank you.

Dinko
Telerik team
commented on 19 Jan 2022, 11:05 AM

Thank you for the provided project and additional details. I have overlooked this behavior and I apologize for that. You are right that the filter applied to a child grid will be applied to all children. The reason is that all child views belong to the same template and the filtering operation applies to all views. A possible solution here is to use Custom Filtering behavior. This way you can control which rows should be visible per the filter criteria no matter which child row it is. This approach will require custom implementation on your side. This way you can control the row but still if the user enters filter text in the filter cell, the text will be applied to all child grid filter cells.

Regarding your second question, I have debugged the project and in this case, the DataSource of the grid is set first then the source of the child template. The CreateRow event handler is subscribed to the event after the population and it won't be triggered for the main parent rows. After debugging the best approach here is to use the RowFormatting event instead. You can subscribe to this event before populating the grid. Also, in the FormatGrid() method you can set the MasterTemplate.DataSource last.

Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        AddHandler RadGridViewRecords.GridViewElement.RowFormatting, AddressOf GridRowFormatting
        InitData()
        FormatGrid()

    Catch ex As Exception
        RadMessageBox.Show(Me.Text, "Error!", MessageBoxButtons.OK, RadMessageIcon.Error, ex.Message)
    End Try
End Sub

Private Sub GridRowFormatting(sender As Object, e As RowFormattingEventArgs)
    Try
        If TypeOf e.RowElement.RowInfo Is GridViewHierarchyRowInfo Then
            Dim info = e.RowElement.RowInfo
            If info.ChildRows.Count = 0 AndAlso TypeOf info.Parent Is MasterGridViewTemplate Then
                info.IsVisible = False
            End If
        End If
    Catch ex As Exception
        RadMessageBox.Show(Me.Text, "Error!", MessageBoxButtons.OK, RadMessageIcon.Error, ex.Message)
    End Try
End Sub

Private Sub FormatGrid()
    Try
        RadGridViewRecords.Relations.Clear()
        Dim relation As New GridViewRelation(Me.RadGridViewRecords.MasterTemplate)
        relation.RelationName = "Records_Relation"
        relation.ParentColumnNames.Add("ParentColumn1")
        relation.ChildColumnNames.Add("ChildColumn1")
        relation.ChildTemplate = GridViewTemplate1
        Me.RadGridViewRecords.Relations.Add(relation)
        GridViewTemplate1.DataSource = bsChild
        RadGridViewRecords.MasterTemplate.DataSource = bsParent

        RadGridViewRecords.TableElement.TableHeaderHeight = 21
    Catch ex As Exception
        Throw
    End Try
End Sub

I hope my reply is helpful.

João
Top achievements
Rank 1
commented on 20 Jan 2022, 06:07 PM | edited

Hello.

Thank you for your reply.

I have tested your solution to hide Parent rows if these do not have Childs and it works as intended.

As for the other issue rergarding the filters, i will test the Custom Filter option you suggested and come back if I find any issues.

Thank you once again.
Tags
GridView
Asked by
João
Top achievements
Rank 1
Answers by
Dinko
Telerik team
Share this question
or