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

RowDataBound e.Row.DataItem RadGrid equivalent

7 Answers 661 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 13 Jan 2013, 10:04 PM
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

7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Jan 2013, 10:04 AM
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
0
Richard
Top achievements
Rank 1
answered on 14 Jan 2013, 11:47 AM
Hi Princy,

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

Thanks,
Richard
0
Princy
Top achievements
Rank 2
answered on 15 Jan 2013, 05:08 AM
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.
0
John
Top achievements
Rank 1
answered on 19 Apr 2013, 02:11 PM
Dear Princy,

Thanks for the code on how to access the cell value in a RadGrid! :)
0
Vipul
Top achievements
Rank 1
answered on 07 Aug 2014, 06:41 PM
Hi Richard 

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



0
Kim
Top achievements
Rank 1
answered on 28 Jun 2016, 07:47 AM
I don't think this works if you have a GridTemplateColumn. How can i get that underlying value? When i inspect it returns ""
0
Eyup
Telerik team
answered on 01 Jul 2016, 07:12 AM
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.
Tags
Grid
Asked by
Richard
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Richard
Top achievements
Rank 1
John
Top achievements
Rank 1
Vipul
Top achievements
Rank 1
Kim
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or