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

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

2 Answers 260 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nabeel
Top achievements
Rank 1
Nabeel asked on 23 Oct 2015, 08:05 AM

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 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Oct 2015, 05:59 PM

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

0
Nabeel
Top achievements
Rank 1
answered on 24 Oct 2015, 07:06 PM
thank Jayesh, it works.
Tags
Grid
Asked by
Nabeel
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Nabeel
Top achievements
Rank 1
Share this question
or