Retrieve color of parent row

4 posts, 1 answers
  1. Jim
    Jim avatar
    2 posts
    Member since:
    Nov 2012

    Posted 08 Jul 2015 Link to this post

    Hello ... I've scoured the forums for an answer to this seemingly simple request. I have a template setup and it's working great. What I want to do is to 'paint' the color of the bounding box, for the child rows, the same color as their 'parent' row. I know I need to use ViewCellFormatting to achieve the painting of the box but I can't figure out how to get the row color (I'm using Conditional Formatting to color the parent rows) from the parent during the ViewCellFormatting event. The CellFormattingEventArgs only seems to expose the HierarchicalRowInfo object which, if I'm correct in my understanding, represents the Logical tree and the color of the row is from the Visual tree so I need the actual RowElement object representing the parent row. Below is the code for painting my bounding box:

    As you can see, I'm using VB.NET but I can always translate C#. I want to replace the Color.GreenYellow with something like e.CellElement.Parent.BackColor. The problem is that the parent is the ScrollableRowsContainer element, not the GridDetailViewRowElement that I'm trying to get. How the heck can I achieve this?

    Thanks, Jim

    If TypeOf e.CellElement Is GridDetailViewCellElement Then
        Dim row As GridRowElement = e.CellElement.Parent 'This line doesn't actually retrieve the BackColor of the Parent!!!!!!
        e.CellElement.DrawFill = True
        e.CellElement.BackColor = Color.GreenYellow
        e.CellElement.Padding = New Padding(9, 4, 3, 4)
        e.CellElement.Margin = New Padding(10, 0, 0, 0)
        e.CellElement.Parent.
    End If

  2. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 09 Jul 2015 Link to this post

    Hello Jim,

    Thank you for writing.

    Here is how you can access the parent row, row element and get its color:
    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)
     
        AddGridBoundToDataTablesWithHierarchy()
        AddHandler radGridView1.ViewCellFormatting, AddressOf radGridView1_ViewCellFormatting
     
        Dim obj As New ConditionalFormattingObject("ID", ConditionTypes.Greater, "5", "", True)
        obj.RowBackColor = Color.Red
        Me.radGridView1.Columns("ID").ConditionalFormattingObjectList.Add(obj)
    End Sub
     
    Private Sub radGridView1_ViewCellFormatting(sender As Object, e As CellFormattingEventArgs)
        If TypeOf e.CellElement Is GridDetailViewCellElement Then
            Dim detailCell As GridDetailViewCellElement = DirectCast(e.CellElement, GridDetailViewCellElement)
            Dim parentRowElement As GridRowElement = radGridView1.TableElement.GetRowElement(TryCast(e.Row, GridViewDetailsRowInfo).Owner)
            If parentRowElement IsNot Nothing AndAlso parentRowElement.GetPropertyValue(LightVisualElement.BackColorProperty).ValueSource <> ValueSource.Style Then
                detailCell.BackColor = parentRowElement.BackColor
                detailCell.GradientStyle = GradientStyles.Solid
            Else
                detailCell.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
                detailCell.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local)
            End If
        End If
    End Sub

    I hope that you find this information useful. Should you have any other questions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jim
    Jim avatar
    2 posts
    Member since:
    Nov 2012

    Posted 09 Jul 2015 in reply to Stefan Link to this post

    Stefan, thanks so much as that gave me what I was looking for. The key was the TableElement property of the GridView (which gave me the GridTableElement) and then the GridTableView's GetRowElement method to get the RowElement from the RowInfo. I'm slowly understanding things but it's tough to find some things in your documentation. I understand that part of that is because your tools are so extensive, it's just difficult to find things at times ... Thanks again!
  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 10 Jul 2015 Link to this post

    No problem Jim, I am glad I could help. In general playing with the visual elements is not recommended as it might cause issues, hence these methods are not exposed in the documentation, but the below usage should be OK.

    Should you have any other questions or suggestions, do not hesitate to contact us.

    Regards,
    Stefan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top