can i access MasterTableView's datakeyvalue in grid ItemCommand event against a DetailTable command

3 posts, 1 answers
  1. Nabeel
    Nabeel avatar
    16 posts
    Member since:
    Sep 2015

    Posted 23 Oct 2015 Link to this post

    The scenarios:

    I have a MasterTableView and it contains one DetailsTable. And the GridTableView contains a button in its CommandItemTemplate section. I want to fetch the parent table ID (MasterTableView) against the button click in the hierarchical child table (GridTableView). The e.CommandArgument property in the grid's ItemCommand event is empty.

     

    The markup for the CommandItemTemplate is below:

     

    <CommandItemTemplate>
                                    <div style="padding: 5px 5px; text-align:right" >
                                        <asp:Button ID="btnSaveAction" runat="server" SkinID="Save" 
                                            CausesValidation="false" Text='​Save'  CommandName="​Save" CommandArgument='<%# Eval("ID") %>' />
                                    </div>
                                </CommandItemTemplate>​

  2. Answer
    Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 23 Oct 2015 in reply to Nabeel Link to this post

    Hello,

    Please try with the below code snippet.

     

    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnItemCommand="RadGrid1_ItemCommand" AutoGenerateColumns="false">
        <MasterTableView DataKeyNames="ID">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name"></telerik:GridBoundColumn>
            </Columns>
            <DetailTables>
                <telerik:GridTableView DataKeyNames="ID" CommandItemDisplay="Top">
                    <CommandItemTemplate>
                        <div style="padding: 5px 5px; text-align: right">
                            <asp:Button ID="btnSaveAction" runat="server" SkinID="Save"
                                CausesValidation="false" Text='​Save' CommandName="​Save" CommandArgument='<%# Eval("ID") %>' />
                        </div>
                    </CommandItemTemplate>
                    <Columns>
                        <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"></telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name"></telerik:GridBoundColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
    </telerik:RadGrid>

    protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
        dynamic data = new[] {
            new { ID = 11, Name ="Name_11"},
            new { ID = 22, Name ="Name_22"}
        };
        e.DetailTableView.DataSource = data;
    }
     
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Save")
        {
            string strParentID = (e.Item.OwnerTableView.ParentItem as GridDataItem).GetDataKeyValue("ID").ToString();
            // Access the master table's datakey here
        }
    }

     

    Let me know if any concern.

    Thanks,

    Jayesh Goyani

  3. Nabeel
    Nabeel avatar
    16 posts
    Member since:
    Sep 2015

    Posted 24 Oct 2015 in reply to Jayesh Goyani Link to this post

    thank Jayesh, it works.
Back to Top