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));
}
}
Thanks
7 Answers, 1 is accepted
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
Thanks for the response but I wanted to access the data object being bound and not a cell value.
Thanks,
Richard
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.
Thanks for the code on how to access the cell value in a RadGrid! :)
Try this
RadGrid.ItemDataBound += delegate(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
var ObjectYouWant = e.Item.DataItem;
}
}
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