Datakeys on ItemDataBound Event

10 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
    2732 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. UI for ASP.NET Ajax is Ready for VS 2017
  4. 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.
  5. 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.
  6. 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...
  7. 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.

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

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

    Posted 17 Oct 2012 Link to this post

    Thank you for giving Snippet
  11. 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


Back to Top
UI for ASP.NET Ajax is Ready for VS 2017