Center align child GridViewTemplate

1 Answer 25 Views
GridView
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Jure asked on 17 Mar 2022, 09:13 AM | edited on 22 Mar 2022, 10:19 AM

Hi.

Is there a way to horizontally align a child GridViewTemplate so that the (BestFitColumns) columns are not on the left but equally distant from left and right, so Center?

Edit:

Jure

 

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 21 Mar 2022, 02:05 PM

Hello Jure,

Thank you for writing.  

In order to indent the child grid, it is necessary to apply Margin to the GridDetailViewCellElement in the ViewCellFormatting event:

private void radGridView1_ViewCellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
    GridDetailViewCellElement detailCell = e.CellElement as GridDetailViewCellElement;
    if (detailCell != null)
    {
        detailCell.Margin = new Padding(50, 0, 0, 0);
    }
}

Give this approach a try and let me know if it works for you. If not may I ask you to share an image of the desired behavior? This way I could better understand it and think of a suitable solution.

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/.

Jure
Top achievements
Rank 2
Iron
Iron
Iron
commented on 22 Mar 2022, 10:21 AM

Thank you for replying Dinko.  Unfortunately it doesn't help. I have edited the original question with a screenshot.
Dinko | Tech Support Engineer
Telerik team
commented on 24 Mar 2022, 01:36 PM

There is not out of box way to position the child template in the center. You could calculate the margin Left value so that the child template position as per your request. Another approach will be to create a custom class that derives from GridDetailViewRowElement and modifies the ArrangeOverride method. Inside you could create your own calculations and set the x starting point of the element. The following code snippet demonstrates what I have in mind.

public  class MyGridDetailViewRowElement : GridDetailViewRowElement
{
    protected override Type ThemeEffectiveType
    {
        get { return typeof(GridDetailViewRowElement); }
    }
    protected override SizeF MeasureOverride(SizeF availableSize)
    {
        this.ContentCell.ChildTableElement.StretchHorizontally = false;
        return base.MeasureOverride(availableSize);
    }
    protected override SizeF ArrangeOverride(SizeF finalSize)
    {
        base.ArrangeOverride(finalSize);

        RectangleF clientRect = GetClientRectangle(finalSize);

        float x = clientRect.Left;

        x = this.TableElement.Size.Width/2 - (clientRect.Left + TableElement.CellSpacing + ContentCell.DesiredSize.Width + this.TableElement.VScrollBar.Size.Width)/2;
           
        ContentCell.Arrange(new RectangleF(x, clientRect.Top, clientRect.Width , clientRect.Height));
        return finalSize;
    }
}
// Subscribe to the CreateRow event of the RadGridView to replace the default // GridDetailViewRowElement
private void GridOrders_CreateRow(object sender, GridViewCreateRowEventArgs e)
{
    if (e.RowType == typeof(GridDetailViewRowElement))
    {
        e.RowElement = new MyGridDetailViewRowElement();
    }
}

Keep in mind that this is a custom solution and it is not fully tested. Feel free to modify it to fit in your application.

 

Tags
GridView
Asked by
Jure
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or