TelerikGridLayoutRef.GridLayoutRows.Add doesn't exist

1 Answer 33 Views
GridLayout
Joel
Top achievements
Rank 2
Iron
Iron
Iron
Joel asked on 04 Feb 2025, 06:03 PM
How do I add a new row to the GridLayout through code?  Then, I'll need to add a new GridLayoutItem to the new row.  Then, I need to add a custom component to that GridLayoutItem.

1 Answer, 1 is accepted

Sort by
0
Hristian Stefanov
Telerik team
answered on 06 Feb 2025, 08:15 AM

Hi Joel,

To dynamically add a new row and a GridLayoutItem containing custom elements to a TelerikGridLayout, you can use conditional rendering in Razor. The TelerikGridLayout does not support direct manipulation of rows or items through code-behind, but you can achieve this by managing the layout through a list of items. Use a list to store your items and iterate over them to render the rows and items dynamically.

Here is a brief example that you can use as a starting point:

<TelerikGridLayout>
    <GridLayoutColumns>
        <GridLayoutColumn Width="200px"></GridLayoutColumn>
        <GridLayoutColumn Width="200px"></GridLayoutColumn>
        <GridLayoutColumn Width="200px"></GridLayoutColumn>
    </GridLayoutColumns>
    <GridLayoutItems>
        @foreach (var row in Rows)
        {
            @foreach (var item in row)
            {
                <GridLayoutItem>
                    <div style="border: 1px gray solid; padding: 5px;">
                        @item
                    </div>
                </GridLayoutItem>
            }
        }
    </GridLayoutItems>
</TelerikGridLayout>

<TelerikButton OnClick="AddRow">Add Row</TelerikButton>

@code {
    private List<List<string>> Rows = new()
    {
        new List<string> { "Item 1", "Item 2", "Item 3" }
    };

    private void AddRow()
    {
        Rows.Add(new List<string> { $"New Item {Rows.Count + 1}" });
    }
}

    Regards,
    Hristian Stefanov
    Progress Telerik

    Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

    Joel
    Top achievements
    Rank 2
    Iron
    Iron
    Iron
    commented on 06 Feb 2025, 03:41 PM

    I like it; its a good solution.  As an old "desktop" guy I frequently forget that I can manipulate the markup right on the presentation layer.
    Tags
    GridLayout
    Asked by
    Joel
    Top achievements
    Rank 2
    Iron
    Iron
    Iron
    Answers by
    Hristian Stefanov
    Telerik team
    Share this question
    or