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

Radgrid in FormTemplate of Radgrid issues

1 Answer 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 17 Feb 2015, 02:58 AM
I've been fighting with this issue for hours and hours now and I'm about ready to throw Rad controls out the window, it's so counter-intuitive.

I have a Radgrid called MyGroupsRadGrid01.  This grid displays a list of groups I'm in.  When I click on the Edit command of one of the rows, it displays the details of the group as well as a RadGrid of the other members in the group called GroupMembersRadGrid all within a FormTemplate.  I want to be able to add or delete an entry in GroupMembersRadGrid using GroupMemberRadGrid_DeleteCommand() and GroupMemberRadGrid_InsertCommand().  I tried using GroupMemberRadGrid_NeedDataSource() but the method itself cannot find GroupMemberRadGrid.  I then tried populating GroupMemberRadGrid within MyGroupsRadGrid01_ItemDataBound but after deleting an entry, the GroupMemberRadGrid showed up as empty (which seems to me necessitate's the GroupMemberRadGrid_NeedDataSource() that I can't get to work.  

<telerik:RadGrid ID="MyGroupsRadGrid01" runat="server"
    OnNeedDataSource="MyGroupsRadGrid01_NeedDataSource"
    AutoGenerateColumns="false" AutoPostBackOnFilter="true" OnUpdateCommand="MyGroupsRadGrid01_UpdateCommand" OnItemDataBound="MyGroupsRadGrid01_ItemDataBound"
    AllowSorting="true" AllowAutomaticUpdates="True" AllowAutomaticInserts="True" OnEditCommand="MyGroupsRadGrid01_EditCommand"
    AllowFilteringByColumn="false" MasterTableView-CommandItemDisplay="Top">
    <MasterTableView DataKeyNames="BWGroupID">
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <table id="Table1" style="margin-left: 50px;" width="100%" cellspacing="2" cellpadding="1" border="0" rules="none" style="border-collapse: collapse;">
                    <tr>
                        <td valign="top">
                            <table id="Table2" cellspacing="2" cellpadding="1" border="0" rules="none" style="border-collapse: collapse;">
                                <tr id="BWGroupIDRow" runat="server">
                                    <td>BW Group ID   </td>
                                    <td><asp:Label ID="BWGroupIDLabel" runat="server" Text='<%# Bind("BWGroupID") %>' /></td>
                                </tr>
                                <tr id="BWGroupNameRow" runat="server">
                                    <td>BW Group Name   </td>
                                    <td><asp:Label ID="Label5" runat="server" Text='<%# Bind("BWGroupName") %>' /></td>
                                </tr>
                                <tr id="BWUserIDRow" runat="server">
                                    <td>BW User ID Owner   </td>
                                    <td><asp:Label ID="Label6" runat="server" Text='<%# Bind("BWUserOwner.BWUserID") %>' /></td>
                                </tr>
                                <tr id="DisplayNameRow" runat="server">
                                    <td>BW User Owner   </td>
                                    <td><asp:Label ID="Label7" runat="server" Text='<%# Bind("BWUserOwner.DisplayName") %>' /></td>
                                </tr>
                                <tr id="IsActive" runat="server">
                                    <td></td>
                                    <td><asp:CheckBox ID="DeactivateCheckBox" runat="server" Text="Deactivate" /></td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2"><br />
                                        <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                            runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button
                                        <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" CommandName="Cancel"></asp:Button>
                                    </td>
                                </tr>
                            </table>
 
                        </td>
                        <td valign="top">
                            <table id="Table3" style="margin-left: 5px;" cellspacing="2" cellpadding="1" border="0" rules="none" style="border-collapse: collapse;">
                                <tr><td><h4>Group Members:</h4></td></tr>
                                <tr>
                                    <td>
                                        <telerik:RadGrid ID="GroupMemberRadGrid" runat="server" width="100%" AllowAutomaticUpdates="True" AllowAutomaticInserts="True"
                                            AllowAutomaticDeletes="true" OnDeleteCommand="GroupMemberRadGrid_DeleteCommand" OnNeedDataSource="GroupMemberRadGrid_NeedDataSource"
                                            AutoPostBackOnFilter="true" OnUpdateCommand="GroupMemberRadGrid_UpdateCommand">
                                            <MasterTableView AutoGenerateColumns="False" DataKeyNames="BWUserId">
                                                <Columns>
                                                    <telerik:GridBoundColumn DataField="BWGroupId" DataType="System.Int32" HeaderText="Group ID" ReadOnly="True" SortExpression="BWGroupId" UniqueName="BWGroupId"></telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="BWUserId" DataType="System.Int32" HeaderText="User ID" ReadOnly="True" SortExpression="BWUserId" UniqueName="BWUserId"></telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="DisplayName" HeaderText="Name" SortExpression="DisplayName" UniqueName="DisplayName"></telerik:GridBoundColumn>
                                                    <telerik:GridButtonColumn Text="Delete" CommandName="Delete" ButtonType="ImageButton" />
                                                </Columns>
                                            </MasterTableView>
                                        </telerik:RadGrid>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
                <br />
            </FormTemplate>
        </EditFormSettings>
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="LinkButton" EditText="View/Edit" UniqueName="EditCommandColumn"></telerik:GridEditCommandColumn>
            <telerik:GridBoundColumn UniqueName="BWGroupID" DataField="BWGroupID" HeaderText="BW Group ID" SortExpression="BWGroupID" FilterControlWidth="150px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BWGroupName" HeaderText="BW Group Name" ReadOnly="true" SortExpression="BWGroupName" FilterControlWidth="150px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BWUserOwner.BWUserID" HeaderText="BW User ID Owner" SortExpression="BWUserOwner.BWUserID" FilterControlWidth="50px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BWUserOwner.DisplayName" HeaderText="BW User Owner" SortExpression="BWUserOwner.DisplayName" FilterControlWidth="50px" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
protected void MyGroupsRadGrid01_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editedItem = e.Item as GridEditableItem;
        Label BWGroupID = editedItem.FindControl("BWGroupIDLabel") as Label;
        int ParseBWGroupID = -1; int.TryParse(BWGroupID.Text, out ParseBWGroupID);
 
        if (ParseBWGroupID < 1)
            return;
 
        BWGroupUsers usersInGroup = new BWGroupUsers();
        usersInGroup.GetActiveBWUsersByGroupID(ParseBWGroupID);
 
        RadGrid GroupMemberRadGrid = new RadGrid();
        GroupMemberRadGrid = (RadGrid)e.Item.FindControl("GroupMemberRadGrid");
 
        ViewState["BWGroupID"] = ParseBWGroupID;
 
        //GroupMemberRadGrid.DataSource = usersInGroup.BWGroupUsersList.OrderBy(u => u.DisplayName);
        //GroupMemberRadGrid.DataBind();
    }
}
 
protected void GroupMemberRadGrid_DeleteCommand(object sender, GridCommandEventArgs e)
{
    if (e.Item is GridDataItem && e.CommandName == "Delete")
    {
        GridDataItem item = (GridDataItem)e.Item;
        int parseGroupId = -1; int.TryParse(item["BWGroupId"].Text.ToString(), out parseGroupId);
        int parseUserId = -1; int.TryParse(item["BWUserId"].Text.ToString(), out parseUserId);
 
        BWGroupUser selectedGroupUser = new BWGroupUser();
        //selectedGroupUser.DeleteBWGroupUser(parseGroupId, parseUserId);
    }
}
 
protected void GroupMemberRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    GroupMemberRadGrid.DataSource = GroupMemberDataSource().BWUserList;  //Doesn't find this!
}
 
private BWUsers GroupMemberDataSource()
{
    BWUsers allBWUsers = new BWUsers();
    if (ViewState["BWGroupID"] != null && ViewState["BWGroupID"].ToString().Length > 0)
    {
        int ParseBWGroupID = -1; int.TryParse(ViewState["BWGroupID"].ToString(), out ParseBWGroupID);
 
        allBWUsers.GetActiveBWUsersByGroupID(ParseBWGroupID);
        return allBWUsers;
    }
    return allBWUsers;
}



It's 9:00pm and I've been working on this since noon.  I can't fight with this all night and it's due in 11 hours.  I need help.

1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 19 Feb 2015, 04:13 PM
Hello,

You should change the code as shown below in order to access the grid:

protected void GroupMemberRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = GroupMemberDataSource().BWUserList;
    }

Additionally, if you want CRUD operations for the grid they should be automatically generated or manually. You should not mix them.

Regards,
Pavlina
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Share this question
or