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

Get the value of Row on ItemDataBound event for a nested view

2 Answers 262 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 10 Jul 2013, 08:33 PM
Hi,

I am trying to convert my asp.net code to rad. On Itemdatabound I want to get the value of the column "id1" specified below. I am specifying id1 as DataKey when defining the rad grid.

 <telerik:RadGrid ID="View1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" GridLines="Horizontal" OnItemDataBound="RadGrid2_ItemDataBound" DataKeyNames="id1" >
<telerik:GridTemplateColumn>
             <ItemTemplate>
                        <a href="javascript:collapseExpand('id1_<%# Eval("d1") %>');">
                            <img id="imageSubId_<%# Eval("id1") %>" alt="Click to show/hide orders" border="0"
                                src="Images/bullet_toggle_plus.jpg" /></a>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="id1" HeaderText="ID"/>
    <telerik:GridTemplateColumn>

Code Behind:
foreach (GridDataItem item in View1.MasterTableView.Items)
                {
                    if (item.IsDataBound)
                    {
                        string oid = item.GetDataKeyValue("id1").ToString();
                        Response.Write(oid);
}
}

When I execute this code I am not getting the value of id1. 

Can someone look into this?

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Jul 2013, 04:24 AM
Hi John,

I guess you want to access the DataKey value. Please try the following code snippet.

ASPX:
<telerik:RadGrid ID="View1" runat="server" onitemdatabound="View1_ItemDataBound">
    <MasterTableView DataKeyNames="id1">
        <Columns>
           . . . . . . . . . . . . .
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void View1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        string oid = item.GetDataKeyValue("id1").ToString();
        Response.Write(oid);
    }       
}

Thanks,
Princy
0
John
Top achievements
Rank 1
answered on 11 Jul 2013, 01:48 PM
Thanks a ton mate.

Got it right.

Thanks,

John
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
John
Top achievements
Rank 1
Share this question
or