This is a migrated thread and some comments may be shown as answers.
Datakeys on ItemDataBound Event
10 Answers 408 Views
This is a migrated thread and some comments may be shown as answers.
S
Top achievements
Rank 1
S asked on 01 Oct 2012, 10:33 AM
Hai,

          Can anyone pls tell me how to get the Datakeynames in Item DataBound Event?Whether i need to give the DataKeyNames in Rad Grid or in Master Table View? Pls Reply as soon as Possible

10 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 01 Oct 2012, 12:57 PM
Hello,


Thanks,
Jayesh Goyani

<MasterTableView DataKeyNames="ID">


protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
             
            string strID = item.GetDataKeyValue("ID").ToString();
           
        }
    }
0
Balakrishna Reddy
Top achievements
Rank 1
answered on 16 Oct 2012, 08:03 AM
GridDataItem item = e.Item as GridDataItem;
             
            string strID = item.GetDataKeyValue("ID").ToString();
if i use this code i am getting NULL Value into item object......
please help me.
0
Shinu
Top achievements
Rank 1
answered on 16 Oct 2012, 08:15 AM
Hi,

Please try another approach which is  shown below.

ASPX:
<MasterTableView DataKeyNames="ID">

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        string Id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString();
    }
}

Thanks,
Shinu.
0
Balakrishna Reddy
Top achievements
Rank 1
answered on 16 Oct 2012, 09:40 AM
Thanks for giving code.
But here also i am getting item as NULL Value only........
Please  help me 


Thanks Inadvance...
0
Shinu
Top achievements
Rank 1
answered on 17 Oct 2012, 05:20 AM
Hi,

Unfortunately i couldn't replicate the issue. Here is the sample code I tried to get the DataKeyValue in ItemDataBound event.

ASPX:
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" runat="server" onitemdatabound="RadGrid1_ItemDataBound">
    <MasterTableView DataKeyNames="OrderID">
        <Columns>
            <telerik:GridBoundColumn UniqueName="ShipName" DataField="ShipName"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="ShipCity" DataField="ShipCity"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string strID = item.GetDataKeyValue("OrderID").ToString();
            //OR
            string Id = item.OwnerTableView.DataKeyValues[item.ItemIndex]["OrderID"].ToString();
        }
}

Please provide the code if it doesn't help.

Thanks,
Shinu.

0
Balakrishna Reddy
Top achievements
Rank 1
answered on 17 Oct 2012, 07:02 AM
Hi,
    Thank u for giving code.Now i want datakey value inside radbutton_Click() event.I am using code like as follows:
protected void radbutton_Click(object sender, EventArgs e)
    {
        RadButton BadOrder = (RadButton)sender;
        GridDataItem item = BadOrder.NamingContainer as GridDataItem;
        string id = Convert.ToString(item.GetDataKeyValue("OrderDetailID"));
       
    }
Then i am getting ID=Null.....
So please help me..........

0
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Oct 2012, 04:51 PM
Hello,

Please try with below code snippet/Demo.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
           >
           <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
               <Columns>
                   <telerik:GridTemplateColumn>
                       <ItemTemplate>
                           <telerik:RadButton ID="RadButton1" runat="server" Text="RadButton1" OnClick="RadButton1_Click">
                           </telerik:RadButton>
                       </ItemTemplate>
                       <EditItemTemplate>
                           <telerik:RadButton ID="RadButton2" runat="server" Text="RadButton2" OnClick="RadButton2_Click">
                           </telerik:RadButton>
                       </EditItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridEditCommandColumn></telerik:GridEditCommandColumn>
               </Columns>
           </MasterTableView>
       </telerik:RadGrid>
protected void RadButton1_Click(object sender, EventArgs e)
        {
            GridDataItem item = (sender as RadButton).NamingContainer as GridDataItem;
            string strId = item.GetDataKeyValue("ID").ToString();
        }
        protected void RadButton2_Click(object sender, EventArgs e)
        {
            GridEditableItem item = (sender as RadButton).NamingContainer as GridEditableItem;
            string strId = item.GetDataKeyValue("ID").ToString();
        }
 
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
              new { ID = 1, Name ="aaa"},
              new { ID = 2, Name = "bbb"},
              new { ID = 3, Name = "ccc"},
              new { ID = 4, Name = "ddd"},
               new { ID = 5, Name ="eee"},
               new { ID = 6, Name ="aaa"},
              new { ID = 7, Name = "bbb"},
              new { ID = 8, Name = "ccc"},
              new { ID = 9, Name = "ddd"},
               new { ID = 10, Name ="eee"}
            };
            RadGrid1.DataSource = data;
        }


Thanks,
Jayesh Goyani
0
Balakrishna Reddy
Top achievements
Rank 1
answered on 18 Oct 2012, 04:58 AM
Thank you for giving Snippet
0
Pravallika
Top achievements
Rank 1
answered on 23 Sep 2014, 02:29 PM
Hai all..................

in radgrid Itemcommand Event i am getting GetDataKeyValue and i am storing it in session after doing some operations like update and delete for some other divisions i am rebinding this grid.after doing rebind also i need that selected row only for that in itemdatabound i  write the code like if session is equal to null then only it will go to that code are else it will select by defalut 1st row only.if session not equal to null then it will check

GridDataItem item = e.Item as GridDataItem;
       if(item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString()==Session["id"])
                  item.SelecteValue=true;
so it will stay in perticular row only but in item.OwnerTableView.DataKeyValues[item.ItemIndex]["ID"].ToString() i am getting 1st row value if i select 2nd row also. in itemcommand i am getting 2nd row id and i am storing it in session but it is not comparing to itemdatabound value how to set selected row only afeter doing rebind also in itemdatabound


0
Kiranmayee
Top achievements
Rank 1
answered on 11 Sep 2018, 04:15 PM

Hi,

Can someone please help me on how to Radgrid2 Data key Values in Radgrid1? Radgrid2 has a detail table. When I try the code below I get error saying 
'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'

 

Protected Sub Radgrid1_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemDataBound
       
        If (TypeOf e.Item Is GridDataItem) Then
            Dim dataItem As GridDataItem = CType(e.Item, GridDataItem)
            Dim strSidPartNum As String = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("SID_PART").ToString
            Dim strLoadPartNum As String = RadGrid2.MasterTableView.DataKeyValues("LOAD_PART").ToString
            Dim strTotalQty As String = RadGrid2.MasterTableView.DataKeyValues(e.Item.ItemIndex)("LOAD_PART_SUM").ToString
            Dim strQtyExp As String = RadGrid1.MasterTableView.DataKeyValues(e.Item.ItemIndex)("QTY_EXP").ToString

            'FORMATTING to a different color to highlight the part num and quantity variation between the SID and LOADS side
            If strSidPartNum = strLoadPartNum Then
                If strTotalQty <> strQtyExp Then
               
                    dataItem.ForeColor = Color.Red
 
                Else
                 
                End If
            End If
        End If

 

Thank you.

Tags
Grid
Asked by
S
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Balakrishna Reddy
Top achievements
Rank 1
Shinu
Top achievements
Rank 1
Pravallika
Top achievements
Rank 1
Kiranmayee
Top achievements
Rank 1
Share this question
or