2

I am using the Telerik RadGrid to display and edit Hierarchical data. As strange as this may sound - one of the requirements is to have a child detail grid open in insert mode when a link in the parent row is clicked. This requires two things. The expansion of the child grid, and getting this grid loaded in insert mode.

This is what I've tried so far using the recommendations from Telerik documentation; However this doesn't work and throws an exception when rebind is called saying that the child detail grid can't find its linqDataSourceControl; however this control does exist while this code below is executing (I checked).

What is the solution to open a child DetailGridView in insert mode from parent row command button?

Markup:

...
    <DetailTables>
        <telerik:GridTableView>
        ...
        <Columns>
            <ItemTemplate>
                <asp:LinkButton ID="addChildVendorRating" runat="server" CommandName="AddNewChildRating" CausesValidation="false" CssClass="normal-link" CommandArgument='<%# Eval("VendorM2MEntityToQualID")%>'>[Add Rating]</asp:LinkButton>
            </ItemTemplate>
        </Columns>
        <DetailTables>
            <!-- I Need this to be be expanded and in insert mode when addChildVendorRating command link is clicked -->
            <telerik:GridTableView>
        </<DetailTables>

Code:

protected void gridRatings_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == "AddNewChildRating") {
                GridDataItem parentRow = e.Item as GridDataItem;
                GridTableView parentGridView = parentRow.GetClosestParentControlByType<GridTableView>();
                RadGrid parentGrid = parentGridView.GetClosestParentControlByType<RadGrid>();

                parentRow.Expanded = true;
                //parentGridView.HierarchyDefaultExpanded = true;
                //parentGridView.DetailTables[0].InsertItem();
                parentGridView.DetailTables[0].IsItemInserted = true;
                parentGridView.DetailTables[0].Rebind();
    }
}
James
  • 12,636
  • 12
  • 67
  • 104

1 Answers1

1

I figured this out, hopefully this will help someone else. The solution is to cast the command handler e.Item to a GridDataItem; which gives you access to the NestedTableViews - the key to the puzzle. This is the detail view that is actually connected with data unlike DetailTables. Once you set the IsItemInserted on that and rebind it all works like charm.

This allows you to expand a section and put the child section in insert mode all in one shot. Here is the completed code below.

    protected void gridRatings_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "AddNewChildRating") {
                    GridDataItem parentRow = e.Item as GridDataItem;
                    GridTableView parentGridView = parentRow.GetClosestParentControlByType<GridTableView>();

                    if (parentGridView != null)
                    {
                        var targetGridView = ((GridDataItem)e.Item).ChildItem.NestedTableViews[0];
                        if (targetGridView != null)
                        {
                            parentRow.Expanded = true;
                            targetGridView.IsItemInserted = true;
                            targetGridView.Rebind();
                        }
                    }

}
James
  • 12,636
  • 12
  • 67
  • 104