This is a migrated thread and some comments may be shown as answers.

RadGrid inside of RadGrid Edit Form Template?

1 Answer 573 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 27 Jan 2020, 09:05 PM

I currently have a RadGrid (MainGrid) that contains a Template Column. This Template Column itself contains a separate RadGrid (SubGrid).

 

The MainGrid has an EditFormTemplate in which I have an ASP.NET PlaceHolder. During MainGrid's ItemCreated event, I check to make sure the Item is in EditMode, instantiate a new RadGrid, and make this new RadGrid equal to the SubGrid (SubGridEdit). During runtime, SubGridEdit displays perfectly fine with all of the correct columns and data, but any attempt during the MainGrid's ItemCommand or UpdateCommand events to access the data rows fails. Everything appears to be fine, but the application is reporting that SubGridEdit has 0 items in its collection. 

 

During MainGrid's ItemCreated event, I have verified that SubGridEdit (a copy of SubGrid that is added to my Placeholder) contains Items in its collection, but once the application moves to the UpdateCommand or ItemCommand events for MainGrid, I'm lost. I'm able to access SubGridEdit by using the FindControl method of the GridEditableItem/GridDataItem, but it's reporting no Items in its collection despite them clearly being displayed.

 

 

In essence, SubGrid contains data that is tied to each record in MainGrid. Editing MainGrid involves also editing SubGrid. In this case, SubGrid contains a few checkboxes that the user should be able to mark or unmark while editing MainGrid. In order to do so, though, I need to be able to access the controls of the RadGrid inside of the EditFormTemplate. Any pointers?

Here's the ItemDataBound and ItemCreated events for the MainGrid and the relevant ASPX (Names/IDs changed to examples for security purposes)

protected void gvOptionsPOP_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem && !e.Item.IsInEditMode)
    {
        GridDataItem item = (GridDataItem)e.Item;
        RadGrid test123 = (RadGrid)item.FindControl("gvRequirements");
        int id = Convert.ToInt32(item["OptionYearID"].Text);
        string _URLAuthority = Request.Url.Authority;
        string _URLScheme = Request.Url.Scheme;
         
        if (_URLAuthority.Contains("localhost"))
        {
            _URLAuthority = _URLAuthority.Replace(Request.Url.Authority, "exampleurl");
        }
        if (_URLScheme == "http")
        {
            _URLScheme = "https";
        }
         
        string _baseURL = _URLScheme + "://" + _URLAuthority + "/SRTD";
        test123.DataSource = ExampleClass.GetData(id, _baseURL);
        test123.Rebind();
 
        CheckBox chk = (CheckBox)item["OptionExecute"].Controls[0];
        LinkButton lnkbutton = (LinkButton)item["EditPOP"].Controls[0];
         
        lnkbutton.Enabled = false;
 
        if (item.ItemIndex == 0 && chk.Checked)
        {
            lnkbutton.Enabled = true;
        }
        else if (chk.Checked)
        {
            foreach (GridDataItem test1 in gvOptionsPOP.Items)
            {
                CheckBox chk2 = (CheckBox)test1["OptionExecute"].Controls[0];
                LinkButton lnkbutton2 = (LinkButton)test1["EditPOP"].Controls[0];
                if (chk2.Checked)
                {
                    lnkbutton2.Enabled = false;
                }
            }
            lnkbutton.Enabled = true;
        }
    }
}
 
protected void gvOptionsPOP_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditFormItem editedItem = (GridEditFormItem)e.Item;
        GridDataItem originalItem = editedItem.ParentItem;
         
        RadGrid test123 = (RadGrid)originalItem.FindControl("gvRequirementsPOP");
        PlaceHolder editPanel = (PlaceHolder)editedItem.FindControl("pnlPlaceHolder");
 
        RadGrid test456 = test123;
 
        foreach (GridDataItem dataItem in test123.Items)
        {
            CheckBox chkbox = (CheckBox)dataItem["CheckboxTemplate"].FindControl("RequirementExecute");
            chkbox.Enabled = true;
        }
 
        test456.Rebind();
    }
}
<telerik:RadGrid ID="gvOptionsPOP" runat="server"
AllowPaging="false"
PageSize="20"
AllowSorting="false"
ShowFooter="false"
ShowStatusBar="false"
AutoGenerateColumns="false"
OnNeedDataSource="gvOptionsPOP_NeedDataSource"
OnItemCommand="gvOptionsPOP_ItemCommand"
OnItemCreated="gvOptionsPOP_ItemCreated"
OnUpdateCommand="gvOptionsPOP_UpdateCommand"
OnItemDataBound="gvOptionsPOP_ItemDataBound"
Width="80%"
GridLines="Both"
ItemStyle-BorderStyle="Solid">
 
    <ClientSettings EnableRowHoverStyle="true"
       EnablePostBackOnRowClick="false" />
 
    <MasterTableView AllowFilteringByColumn="false"
       ShowFooter="false"
       ShowHeadersWhenNoRecords="true" BorderStyle="Solid">
 
       <EditFormSettings EditFormType="Template">
              <FormTemplate>
                     <br />
                     <asp:PlaceHolder ID="pnlPlaceHolder" runat="server" />
                     <br />
                     <span>
                            <asp:Button runat="server" ID="btnUpdate" Text="Update" CommandName="Update" />
                            <asp:Button runat="server" ID="btnCancel" Text="Cancel" CommandName="Cancel" />
                     </span>
              </FormTemplate>
       </EditFormSettings>
 
       <NoRecordsTemplate>
              <div>NO OPTIONS FOUND.</div>
       </NoRecordsTemplate>
 
       <Columns>
              <telerik:GridButtonColumn UniqueName="EditPOP" CommandName="Edit" Text="Edit" />
              <telerik:GridBoundColumn UniqueName="OptionYearID" DataField="options_id" HeaderText="Option Year ID" ItemStyle-HorizontalAlign="Center" Display="false" HeaderStyle-HorizontalAlign="Center" />
              <telerik:GridBoundColumn UniqueName="OptionYear" DataField="option_period" HeaderText="Option Year" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
              <telerik:GridDateTimeColumn UniqueName="POPStart" DataField="option_award_date" HeaderText="Proposed/Actual Start Date" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
              <telerik:GridDateTimeColumn UniqueName="POPEnd" DataField="option_end_date" HeaderText="Proposed/Actual End Date" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
              <telerik:GridTemplateColumn UniqueName="RequirementTemplate" HeaderText="Requirement/CSA#" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
              <ItemTemplate>
                     <telerik:RadGrid ID="gvRequirementsPOP" runat="server"
                     AllowPaging="false"
                     PageSize="20"
                     AllowSorting="false"
                     ShowFooter="false"
                     ShowStatusBar="false"
                     AutoGenerateColumns="false"
                     OnItemDataBound="gvRequirementsPOP_ItemDataBound"
                     Width="100%"
                     ItemStyle-BorderStyle="Solid">
 
                     <ClientSettings EnableRowHoverStyle="true"
                            EnablePostBackOnRowClick="false" />
 
                     <MasterTableView AllowFilteringByColumn="false"
                            ShowFooter="false"
                            ShowHeadersWhenNoRecords="true" BorderStyle="Solid" DataKeyNames="ReqCSA, EXECUTION_FLAG">
 
                            <NoRecordsTemplate>
                                   <div>NO REQUIREMENTS FOUND.</div>
                            </NoRecordsTemplate>
 
                            <Columns>
                                   <telerik:GridBoundColumn UniqueName="RequirementsID" DataField="Requirements_ID" HeaderText="Requirements ID" Display="false" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
                                   <telerik:GridBoundColumn UniqueName="ExecutionFlag" DataField="EXECUTION_FLAG" HeaderText="Execution Flag" Display="false" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
                                   <telerik:GridHyperLinkColumn UniqueName="ReqLink" DataTextFormatString="{0}" DataNavigateUrlFields="ReqLink" DataNavigateUrlFormatString="{0}" DataTextField="ReqCSA" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" HeaderText="SCA#"  ItemStyle-Width="25%" HeaderStyle-Width="25%" />
                                   <telerik:GridTemplateColumn UniqueName="CheckboxTemplate" HeaderText="Execute" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="25%" HeaderStyle-Width="25%" >
                                          <ItemTemplate>
                                                 <asp:CheckBox ID="RequirementExecute" runat="server" Text="Execute" />
                                          </ItemTemplate>
                                   </telerik:GridTemplateColumn>
                                   <telerik:GridBoundColumn UniqueName="RequirementType" DataField="REQUIREMENT_TYPE" HeaderText="Type" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="25%" HeaderStyle-Width="25%" />
                                   <telerik:GridBoundColumn UniqueName="RequirementStatus" DataField="REQUIREMENT_Status" HeaderText="Status" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="25%" HeaderStyle-Width="25%" />
                            </Columns>
                            </MasterTableView>
                     </telerik:RadGrid>
              </ItemTemplate>
              </telerik:GridTemplateColumn>
              <telerik:GridCheckBoxColumn UniqueName="OptionExecute" DataField="EXECUTION_FLAG" HeaderText="Option Execute" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
       </Columns>
    </MasterTableView>
</telerik:RadGrid>

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 30 Jan 2020, 12:12 PM

Hi Chris,

 

This is probably related to the data binding of the inner grid. You can try using NeedDataSource event handler to resolve that:
https://www.telerik.com/support/kb/aspnet-ajax/grid/details/how-to-bind-radgrid-properly-on-server-side

In this case NeedDataSource can be insufficient and you can try adding DataBinding as well with the same code. You can check a working demonstration here:
https://www.telerik.com/support/code-library/radgrid-nestedviewtemplate-hierarchy-with-programmatic-binding-and-accessing-controls

I hope this will prove helpful.

 

Regards,
Eyup
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or