RowDataBound e.Row.DataItem RadGrid equivalent

8 posts, 0 answers
  1. Richard
    Richard avatar
    9 posts
    Member since:
    Jun 2010

    Posted 13 Jan 2013 Link to this post

    In the past I have used something like this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

            {

                if (e.Row.RowType == DataControlRowType.DataRow)

                {

                    var dr = (ObjectTypeBeingBound)e.Row.DataItem;

                    HyperLink editLink = (HyperLink)e.Row.FindControl("hlEditLink");

                    editLink.Attributes.Add("onclick", String.Format("openRadWindow({0},{1})", dr.Id, dr.Name));

                }

            }

    How do I do similar with a RadGrid?  My best attempt (not an exact replacement for the above method) so far is:

    protected void gvSpecs_ItemDataBound(object sender, GridItemEventArgs e)

            {

                if (e.Item is GridDataItem)

                {

                    GridDataItem item = (GridDataItem)e.Item;

                    var supplierPartNumber = item.GetDataKeyValue("SupplierPartNumber");

                    var setNo = item.GetDataKeyValue("SetNo");

                    HyperLink editLink = (HyperLink)item["editAttributes"].Controls[0];

                    editLink.Attributes.Add("onclick", String.Format("openRadWindow({0},{1})"

                        supplierPartNumber != null ? supplierPartNumber: String.Empty, 

                        setNo != null ? setNo : String.Empty));                

                }

            }


    But I can seem to only access the datakeys and not the rest of the datasource cast as an object.

    Thanks
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 14 Jan 2013 Link to this post

    Hi,

    Try the following code to access cell values in RadGrid.
    C#:
    protected void gvSpecs_ItemDataBound(object sender, GridItemEventArgs e)
    {
     if (e.Item is GridDataItem)
     {
       GridDataItem item = (GridDataItem)e.Item;
       TableCell cell = (TableCell)item["Uniquename"];
       string value = cell.Text;
     }
    }

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Richard
    Richard avatar
    9 posts
    Member since:
    Jun 2010

    Posted 14 Jan 2013 Link to this post

    Hi Princy,

    Thanks for the response but I wanted to access the data object being bound and not a cell value.

    Thanks,
    Richard
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 14 Jan 2013 Link to this post

    Hi,

    Unfortunately I didn't understand your requirement. Data bound to a row in RadGrid are normally accessed using TableCell or using FindControl(if controls are inside Template column). Can you specify what is the 'ObjectTypeBeingBound' in the 'var dr = (ObjectTypeBeingBound)e.Row.DataItem;'. Please provide the full code to understand your requirement.
    Here is a sample code I have created.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" onitemdatabound="RadGrid1_ItemDataBound">
        <MasterTableView>
            <Columns>
                <telerik:GridBoundColumn DataField="OrderID" UniqueName="OrderID">
                </telerik:GridBoundColumn>
                <telerik:GridHyperLinkColumn DataNavigateUrlFormatString="ShipCity" UniqueName="ShipCity">
                </telerik:GridHyperLinkColumn>
            </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;
            TableCell cell = (TableCell)item["Uniquename"]; //accessing Table cell
            string value = DataBinder.Eval(e.Item.DataItem, "DataField").ToString();
        }
    }

    Thanks,
    Princy.
  6. John
    John avatar
    1 posts
    Member since:
    Jan 2013

    Posted 19 Apr 2013 Link to this post

    Dear Princy,

    Thanks for the code on how to access the cell value in a RadGrid! :)
  7. Vipul
    Vipul avatar
    3 posts
    Member since:
    May 2013

    Posted 07 Aug 2014 Link to this post

    Hi Richard 

    Try this
         
    RadGrid.ItemDataBound += delegate(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
              var ObjectYouWant = e.Item.DataItem;
        }
    }



  8. Kim
    Kim avatar
    23 posts
    Member since:
    Dec 2012

    Posted 28 Jun Link to this post

    I don't think this works if you have a GridTemplateColumn. How can i get that underlying value? When i inspect it returns ""
  9. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 01 Jul Link to this post

    Hi Kim,

    You can use the DataBinder.Eval method to achieve this requirement. Please bear in mind that the DataItem object is available only during the ItemDataBound event handler:
    http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values

    I hope this will prove helpful.

    Regards,
    Eyup
    Telerik
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017