Datakeys on ItemDataBound Event

11 posts, 0 answers
  1. S
    S avatar
    7 posts
    Member since:
    Sep 2012

    Posted 01 Oct 2012 Link to this post

    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
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 01 Oct 2012 Link to this post

    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();
               
            }
        }
  3. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 16 Oct 2012 Link to this post

    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.
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Oct 2012 Link to this post

    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.
  5. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 16 Oct 2012 Link to this post

    Thanks for giving code.
    But here also i am getting item as NULL Value only........
    Please  help me 


    Thanks Inadvance...
  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 17 Oct 2012 Link to this post

    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.

  7. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 17 Oct 2012 Link to this post

    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..........

  8. Jayesh Goyani
    Jayesh Goyani avatar
    2733 posts
    Member since:
    May 2010

    Posted 17 Oct 2012 Link to this post

    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
  9. Balakrishna Reddy
    Balakrishna Reddy avatar
    37 posts
    Member since:
    Oct 2012

    Posted 17 Oct 2012 Link to this post

    Thank you for giving Snippet
  10. Pravallika
    Pravallika avatar
    30 posts
    Member since:
    Mar 2014

    Posted 23 Sep 2014 Link to this post

    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


  11. Kiranmayee
    Kiranmayee avatar
    22 posts
    Member since:
    Jul 2018

    Posted 11 Sep 2018 Link to this post

    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.

Back to Top