Questions on RadGrid NestedViewTemplate

3 posts, 0 answers
  1. thecruciable
    thecruciable avatar
    8 posts
    Member since:
    Mar 2007

    Posted 16 Oct 2009 Link to this post

    Hello I looked at an example here on how nested view template is used to have multiple grids. I am currently working on a grid using nested view template and have few question. Before I start my question here is my code:

    ASPX:

    <telerik:RadGrid runat="server" ID="rgCommittee" GridLines="None" AllowSorting="true"
                    AllowPaging="true" AllowFilteringByColumn="true" PageSize="30" AutoGenerateColumns="false"
                    ShowGroupPanel="true" ShowFooter="true" OnNeedDataSource="rgCommittee_NeedDataSource"
                    OnItemDataBound="rgCommittee_ItemDataBound" OnItemCommand="rgCommittee_ItemCommand"
                    OnItemCreated="rgCommittee_ItemCreated">
                    <MasterTableView DataKeyNames="CommitteeID,CommitteeTypeID" AllowMultiColumnSorting="true"
                        GroupLoadMode="Server">
                        <NestedViewTemplate>
                            <asp:Panel ID="pnlMemberList" runat="server" Visible="true">
                                <telerik:RadGrid runat="server" ID="rgCommitteeMember" GridLines="None" AllowSorting="true"
                                    AllowPaging="true" AllowFilteringByColumn="true" PageSize="30" AutoGenerateColumns="false"
                                    ShowGroupPanel="true" ShowFooter="true" OnNeedDataSource="rgCommitteeMember_NeedDataSource"
                                    OnItemCommand="rgCommitteeMember_ItemCommand" OnItemDataBound="rgCommitteeMember_ItemDataBound">
                                    <ValidationSettings EnableValidation="true" ValidationGroup="vgGrid" />
                                    <MasterTableView DataKeyNames="CommitteeMemberID,PersonID" HierarchyLoadMode="SeverOnDemand"
                                        EditMode="InPlace" CommandItemDisplay="TopAndBottom" CommandItemSettings-AddNewRecordText="Add new committee member(s)">
                                        <Columns>
                                            <telerik:GridBoundColumn HeaderText="Member TypeID" AllowFiltering="true" DataField="CommitteeMemberID"
                                                SortExpression="CommitteeMemberID" Visible="false" />
                                            <telerik:GridEditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel"
                                                EditText="Edit" />
                                            <telerik:GridButtonColumn ButtonType="LinkButton" ConfirmText="Are you sure you want to delete this record?"
                                                CommandName="Delete" Text="Delete" UniqueName="DeleteColumn" />
                                            <telerik:GridTemplateColumn>
                                                <EditItemTemplate>
                                                    <PersonSearch:GridPersonSearch ID="gpsMemberName" runat="server" SearchType="Employee" ReturnField="PersonID"
                                                        TwoColumnSearch="false" RequiredFlag="false" />
                                                </EditItemTemplate>
                                                <ItemTemplate>
                                                    <asp:Label runat="server" ID="lblEmpName" />
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                        </Columns>
                                        <NoRecordsTemplate>
                                            <div>
                                                There are no member(s) for this committee.</div>
                                        </NoRecordsTemplate>
                                    </MasterTableView>
                                </telerik:RadGrid>
                            </asp:Panel>
                        </NestedViewTemplate>
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="Committee TypeID" AllowFiltering="true" DataField="CommitteeTypeID"
                                SortExpression="CommitteeTypeID" Visible="false" />
                            <telerik:GridBoundColumn HeaderText="Committee Type" AllowFiltering="true" DataField="CommitteeTypeName"
                                SortExpression="CommitteeTypeName" />                       
                        </Columns>
                        <NoRecordsTemplate>
                            <div>
                                There are no committee assigned to you.</div>
                        </NoRecordsTemplate>
                    </MasterTableView>
                </telerik:RadGrid>
    C# code:
            protected void rgCommittee_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
            {
                rgCommittee.DataSource = Data.Committee.GetList(PersonID);
            }

            protected void rgCommittee_ItemDataBound(object source, GridItemEventArgs e)
            {
                GridDataItem gdiItem = e.Item as GridDataItem;

                if (gdiItem == null)
                {
                    return;
                }
                else
                {
                    if(!gdiItem.IsInEditMode)
                    {
                        //if ((DataBinder.Eval(gdiItem.DataItem, "EmpName") == null) && (DataBinder.Eval(gdiItem.DataItem, "ChairFlag") == null))
                        //{
                            
                        //}

                    }
                }
            }

            protected void rgCommittee_ItemCommand(object source, GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.ExpandCollapseCommandName)
                {
                    ((GridDataItem)e.Item).ChildItem.FindControl("pnlMemberList").Visible = !e.Item.Expanded;
                }
            }

            protected void rgCommittee_ItemCreated(object source, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    intCommitteeID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CommitteeID"].ToString());
                }

                if (e.Item is GridNestedViewItem)
                {
                    e.Item.FindControl("pnlMemberList").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;
                }
            }

            protected void rgCommitteeMember_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
            {
                RadGrid rgCommitteeMember = source as RadGrid;
                rgCommitteeMember.DataSource = Data.CommitteeMember.GetList();
            }

            protected void rgCommitteeMember_ItemDataBound(object source, GridItemEventArgs e)
            {
                if (e.Item is GridDataInsertItem)
                {
                    if (e.Item.IsInEditMode)
                    {
                        #region Declare Controls

                        UserControls.GridPersonSearch gpsMemberName = (UserControls.GridPersonSearch)e.Item.FindControl("gpsMemberName");

                        #endregion

                        #region Bind Controls

                        int intPersonID;
                        if (gpsMemberName != null && e.Item.ItemIndex != -1 &&
                            e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PersonID"] != null &&
                            int.TryParse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PersonID"].ToString(), out intPersonID))
                        {
                            gpsMemberName.SetDropDownPersonID = intPersonID;
                        }
                        #endregion

                    }
                }
                else if (e.Item is GridDataItem)
                {
                    #region Controls

                    if (!e.Item.IsInEditMode)
                    {
                        // view mode (Label controls)
                        ((Label)e.Item.FindControl("lblEmpName")).Text = DataBinder.Eval(e.Item.DataItem, "EmpName") != null ? DataBinder.Eval(e.Item.DataItem, "EmpName").ToString() : string.Empty;
                    }
                    else
                    {
                        // edit mode

                        #region Declare Controls

                        //Committee Type un-editable column.
                        UserControls.GridPersonSearch gpsMemberName = (UserControls.GridPersonSearch)e.Item.FindControl("gpsMemberName");

                        #endregion

                        #region Bind Controls

                        int intPersonID;
                        if (gpsMemberName != null && e.Item.ItemIndex != -1 &&
                            e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PersonID"] != null &&
                            int.TryParse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["PersonID"].ToString(), out intPersonID))
                        {
                            gpsMemberName.SetDropDownPersonID = intPersonID;
                        }

                        #endregion
                    }

                    #endregion
                }
            }

            protected void rgCommitteeMember_ItemCommand(object sender, GridCommandEventArgs e)
            {
                //int intRoleID;
                GridDataItem gdiItem = e.Item as GridDataItem;

                if (gdiItem == null)
                {
                    return;
                }

                if (e.Item.IsInEditMode)
                {
                    UserControls.GridPersonSearch gpsMemberName = (UserControls.GridPersonSearch)e.Item.FindControl("gpsMemberName");

                    switch (e.CommandName)
                    {
                        case "PerformInsert":
                            Page.Validate("vgGrid");

                            if (Page.IsValid)
                            {
                               
                            }
                           else
                           {
                              rgCommitteeList.Controls.Add(new LiteralControl(string.Format("<span class=\"ErrorText\">There was an error inserting the record.</span>")));
                           }
                            break;
                        case "Update":
                            Page.Validate("vgGrid");

                            if (Page.IsValid)
                            {                          
                               
                            }
                            else
                            {
                               rgCommitteeList.Controls.Add(new LiteralControl(string.Format("<span class=\"ErrorText\">There was an error updating the record.</span>")));
                            }
                            break;
                    }
                }          
            }

    The parent Grid displays Committee Name for example student committee. When I click on the student committee it loads the rad grid inside the nested view template that displays me the names of the student associated to that committee.

    If there aren't any students for that committee it should display the message from no records template.

    My questions are:

    When the I call the child rad grid from the nested view template it does not display message from the no record template.
    Secondly if there is data it does not display data either on the child grid.
    But when I click the add button on the child rad grid it fires the NeedDataSource Event on the Child grid and displays the no records template message or displays student data then!

    Can you please tell me how do I handle this, since after I  click the committee name it should call the need data source event on child but it isn't doing that for some reason can you kindly let me know about it.

    Thanks and Regards
    Amitesh
  2. Prangadj
    Prangadj avatar
    174 posts
    Member since:
    Jan 2008

    Posted 22 Oct 2009 Link to this post

    Amitesh, if you set proper query in Data.Committee.GetList(PersonID) method by person id it should return subset of records for this id. Test what items this returns. And to display the no records data, put empty data source in the NeedDataSource handler when needed:

    protected void rgCommittee_NeedDataSource(object source, GridNeedDataSourceEventArgs e)  
     {  
       //if records  
                rgCommittee.DataSource = Data.Committee.GetList(PersonID);  
      //if no data  
             rgCommittee.DataSource = new Objeect[0];  

    Prangadj
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. thecruciable
    thecruciable avatar
    8 posts
    Member since:
    Mar 2007

    Posted 29 Oct 2009 Link to this post

    Hello Prangadj,

    Thanks for the reply. The code you sent me did not work. Because rgCommittee_NeedDataSource is the parent rad grid's Need Data Source and it works absolutely fine in my code. The problem I am having is that when I click expand column on the parent rad grid it loads the chidl rad grid from nested view template as shown in my code and it is here that I cannot display records or No records text because the rgCommitteeMember_NeedDataSource event is not firing and this is the child grid's Need Data source event handler. Can you kindly tell me why this happens. Also one more point to mention when I set my child grid's data source in my parent grids Item created event everything works fine. But I want the code to fire from child grid's need source event.
Back to Top