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

DataKey problem in grid with hierarchy

1 Answer 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Torben Frølund
Top achievements
Rank 1
Torben Frølund asked on 25 Nov 2008, 03:16 PM
Has anyone experienced problems with DataKeys in grids hierachical grids? I have a grid somewhat like this:

Role1
 - User1
 - User2
Role2
 - User2
 - User4
Role 3
 - User1

On the parent level I have a column with a LinkButton and in the ItemCommand event I want to get the DataKey value for the row (eg. "RoleId1") when the button is pressed. This works fine until I click a role and and exand to see users. If I click Role1 row button the detail view is expaned and data is fetched for the details view. In the ItemCommand event I can get the DataKey for Role1, but if I subsequently click the button on Role2 or Role3 their DataKey is missing and when I look into the Item object its seems as though the item is one of their siblings.

Hope somebody has a clue, thanks in advance!

        <telerik:RadGrid id="uiRoleSummaryRadGrid" runat="server" allowsorting="True" width="95%"
            cssclass="GVTableLayout3" skin="" enableajax="True" showfooter="True" allowpaging="True"
            allowmultirowselection="True" gridlines="None" autogeneratecolumns="false" onitemcommand="uiRoleSummaryRadGrid_ItemCommand"
            ondetailtabledatabind="uiRoleSummaryRadGrid_DetailTableDataBind"
            onneeddatasource="uiRoleSummaryRadGrid_NeedDataSource" Font-Size="Medium"
            ForeColor="Red">
            <PagerStyle Mode="NumericPages"></PagerStyle>
            <MasterTableView DataKeyNames="Id,Name">
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="ProfileUserId,ContactFullName" Name="UserDetails" Width="100%">
                        <RowIndicatorColumn Visible="False">
                            <HeaderStyle Width="20px"></HeaderStyle>
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn Visible="False" Resizable="False">
                        <HeaderStyle Width="20px"></HeaderStyle>
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridTemplateColumn UniqueName="ContactFullName" HeaderText="UserName" SortExpression="ContactFullName"
                            HeaderButtonType="LinkButton" SortAscImageUrl="~/SelfServiceB2B/Test/Images/asc.gif"
                            SortDescImageUrl="~/SelfServiceB2B/Test/Images/desc.gif" ItemStyle-ForeColor ="Black" >
                            <ItemTemplate>
                                <asp:LinkButton ID="uiViewRoleNameLinkButton" runat="server" Text='<%# Eval("ContactFullName") %>'
                                    CommandName="ViewUser" CommandArgument='<%# Eval("ProfileUserId") %>' ></asp:LinkButton>
                            </ItemTemplate>
                            <ItemStyle ForeColor="Black"></ItemStyle>
                            </telerik:GridTemplateColumn>
                        </Columns>

                        <EditFormSettings>
                            <PopUpSettings ScrollBars="None"></PopUpSettings>
                        </EditFormSettings>
                    </telerik:GridTableView>
                </DetailTables>
                <RowIndicatorColumn Visible="False">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn Resizable="False">
                <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridTemplateColumn UniqueName="Name" HeaderText="Role" SortExpression="Name"
                        HeaderButtonType="LinkButton" SortAscImageUrl="~/SelfServiceB2B/Test/Images/asc.gif"
                        SortDescImageUrl="~/SelfServiceB2B/Test/Images/desc.gif">
                        <ItemTemplate>
                            <asp:LinkButton ID="uiViewRoleLinkButton" runat="server" Text='<%# Eval("Name") %>'
                                CommandName="ViewRole"></asp:LinkButton>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridButtonColumn ButtonType=PushButton Text = "Edit" UniqueName="ID"
                        HeaderText="Edit" CommandName="EditRole" HeaderButtonType="LinkButton">
                    </telerik:GridButtonColumn>
                    <telerik:GridButtonColumn ButtonType=PushButton Text = "Show Id" UniqueName="ID"
                        HeaderText="Delete" CommandName="DeleteRole" HeaderButtonType="LinkButton">
                    </telerik:GridButtonColumn>
                </Columns>
                <EditFormSettings>
                    <PopUpSettings ScrollBars="None"></PopUpSettings>
                </EditFormSettings>
            </MasterTableView>
            <PagerStyle PageButtonCount="5" AlwaysVisible="true" Mode="NextPrevAndNumeric" HorizontalAlign="Center" Position="Bottom" ShowPagerText="false" />
        </telerik:RadGrid>



        protected void uiRoleSummaryRadGrid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "DeleteRole":
                        {

                            Telerik.Web.UI.GridDataItem item = uiRoleSummaryRadGrid.Items[e.Item.ItemIndex];

                            if (null != item.GetDataKeyValue("Id"))
                            {
                                uiResultLabel.Text = "Role Id" + item.GetDataKeyValue("Id").ToString() ;
                            }
                            else
                            {
                                if (null != item.GetDataKeyValue("ProfileUserId"))
                                {
                                    uiResultLabel.Text = "Error while get data key value for Id, but actual key value is " + item.GetDataKeyValue("ProfileUserId").ToString();
                                }
                                else
                                {
                                    uiResultLabel.Text = "Error while get data key values";
                                }
                              
                            }
                        }
                        break;
                }
            }
            catch (Exception ex)
            {

            }
        }

1 Answer, 1 is accepted

Sort by
0
Accepted
Vlad
Telerik team
answered on 25 Nov 2008, 03:34 PM
Hello Michael,

Please try e.Item.ItemIndexHierarchical instead. The grid Items collection is hierarchical.

All the best,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Torben Frølund
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Share this question
or