Accessing rows
The GridTableView object has an Items property that contains all the data rows in the table view. Each row is represented by a GridDataItem or GridEditFormItem object, depending on whether the row is an edit form. The GridDataItem or GridEditFormItem has an ItemIndex property that is its index in the Items property collection.
When implementing an event handler for an event such as ItemCreated, ItemDataBound, ItemCommand, UpdateCommand, InsertCommand or DeleteCommand, you can obtain a GridDataItem or GridEditFormItem for the row from the event arguments (e.Item or, in a hierarchical grid, e.Item.OwnerTableView.ParentItem).
ItemCreated
//e is the event argument object
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
}
else if (e.Item is GridEditFormItem)
{
GridEditFormItem editItem = e.Item as GridEditFormItem;
}
}
ItemDataBound
// e is the event argument object
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem dataItem = e.Item as GridDataItem;
}
else if (e.Item is GridEditFormItem)
{
GridEditFormItem editItem = e.Item as GridEditFormItem;
}
}
PreRender
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
RadGrid grid = (RadGrid)sender;
foreach (GridDataItem dataItem in grid.Items)
{
}
}
Once you have reference to a certain GridItem you can reach the data in its cells and get references to its embedded controls by following the instructions in the Accessing Cells and Values and Accessing Controls articles.
Accessing items by Looping through the Items collection from a Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
foreach (GridDataItem dataItem in RadGrid1.Items)
{
// do something
}
foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
{
// do something
}
}
Accessing Items by Index on a Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
GridDataItem dataItem = RadGrid1.Items[0];
}
In order to have access to all the items/rows in all Tables/DetailTables in the Hierarchy, the rows/items must be expanded propor to accessing them or the HierarchyLoadMode set to Client.
Accessing all items in the Hierarchy at once on Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
GridDataItemCollection allItemsHierarchy = RadGrid1.MasterTableView.ItemsHierarchy;
}
Iterating through the rows of tables on a 3 level hierarchy on Button Click
Sample Code Snippets for a three level hierarchy can be found at Programmatic Data Binding Using the NeedDataSource Event - Three level Hierarchy
protected void RadButton1_Click(object sender, EventArgs e)
{
GridTableView CustomersTbl = RadGrid1.MasterTableView;
// for each row in Customers Table
foreach (GridDataItem customerItem in CustomersTbl.Items)
{
// if has Child items
if (customerItem.HasChildItems)
{
// Get the Nested Table of that row
GridTableView OrdersTbl = customerItem.ChildItem.NestedTableViews[0];
// for each row in the Nested Table Orders Table.
foreach (GridDataItem orderItem in OrdersTbl.Items)
{
if (orderItem.HasChildItems)
{
// Get the Nested Table of that row
GridTableView OrderDetailsTbl = orderItem.ChildItem.NestedTableViews[0];
// for each row in the Nested Table OrderDetails Table.
foreach (GridDataItem orderDetailItem in OrderDetailsTbl.Items)
{
// do something
}
}
}
}
}
}
Accessing the parent row of a DetailTable by clicking on a Button inside the DetailTable
protected void RadButton1_Click(object sender, EventArgs e)
{
// Button in Template column of Table Level3
RadButton btn = (RadButton)sender;
// Row in which the Button is located at
GridDataItem currentRow = btn.NamingContainer as GridDataItem;
// Table level 3
GridTableView currentTable = currentRow.OwnerTableView;
// NestedViewItem for Table level 3
GridNestedViewItem currentNestedViewItem = currentTable.NamingContainer as GridNestedViewItem;
// Row of Table level 2
GridDataItem parentRow = currentNestedViewItem.ParentItem;
}
Accessing GroupHeaderItem
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
var grid = (RadGrid)sender;
foreach (GridGroupHeaderItem groupHeader in grid.MasterTableView.GetItems(GridItemType.GroupHeader))
{
var aggreGateValue = (int)groupHeader.AggregatesValues["ShipCountry"];
var lblItemCount = groupHeader.FindControl("lblGroupItemCount") as Label;
lblItemCount.Text = string.Format("({0})", aggreGateValue.ToString());
}
}
Accessing GridDetailTemplateItem
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
var grid = (RadGrid)sender;
var detailTemplateItems = grid.MasterTableView.GetItems(GridItemType.DetailTemplateItem);
foreach (GridDetailTemplateItem detailTemplateItem in detailTemplateItems)
{
detailTemplateItem.BackColor = Color.LightGray;
}
}
Hiding GridDetailTemplateItem when exporting
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
var grid = (RadGrid)sender;
if (grid.IsExporting)
{
var detailTemplateItems = grid.MasterTableView.GetItems(GridItemType.DetailTemplateItem);
foreach (GridDetailTemplateItem detailTemplateItem in detailTemplateItems)
{
detailTemplateItem.Visible = false;
}
}
}
Edit items
Items in edit mode are contained in the EditItems property of the RadGrid object.
Accessing the Edit item on Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
// Insert Item
if (RadGrid1.MasterTableView.IsItemInserted)
{
GridEditableItem insertItem = RadGrid1.MasterTableView.GetInsertItem();
}
// Edit Item
GridItemCollection editItems = RadGrid1.EditItems;
foreach(GridEditableItem editItem in editItems)
{
}
}
Accessing the Edit Item when the Grid enters into Insert/Edit Mode
The type of the editable item is can differ depending on the EditMode set to the MasterTableView.
- In WebForms and PopUp edit mode, the insert item is GridEditFormInsertItem and edit item is GridEditFormItem.
- When using InPlace edit mode, the insert item comes as GridDataInsertItem and the edit item as GridDataItem.
private void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if(e.Item is GridEditableItem && e.Item.IsInEditMode)
{
if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
{
// insert item
}
else
{
// edit item
if (e.Item is GridEditFormItem) //EditMode WebForms/PopUp
{
// edit item containig the controls for editing
GridEditFormItem editItem = (e.Item as GridEditFormItem);
// respective data item containing the cells with populated text
GridDataItem dataItem = editItem.ParentItem as GridDataItem;
}
else if (e.Item is GridDataItem) //EditMode InPlace
{
// edit item containing the controls for editing
GridDataItem editItem = (e.Item as GridDataItem);
}
}
}
}
private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
{
// insert item
}
else
{
// edit item
if (e.Item is GridEditFormItem) //EditMode WebForms/PopUp
{
// edit item containig the controls for editing
GridEditFormItem editItem = (e.Item as GridEditFormItem);
// respective data item containing the cells with populated text
GridDataItem dataItem = editItem.ParentItem as GridDataItem;
}
else if (e.Item is GridDataItem) //EditMode InPlace
{
// edit item containing the controls for editing
GridDataItem editItem = (e.Item as GridDataItem);
}
}
}
Accessing Selected items from a Button Click
For convenient proccesing of selected items on the server-side, RadGrid exposes the SelectedItems collection. It contains the selected items in all nested tables inside the grid. To reach the selected items in a certain GridTableView(e.g. MasterTableView) use the GetSelectedItems() method. By using the ChildSelectedItems property you can get collection of the selected items in the GridTableView, including the selected ones from its embedded DetailTable(s) (if such exist).
protected void RadButton1_Click(object sender, EventArgs e)
{
//Get all selected items inside the RadGrid
foreach (GridDataItem selectedDataItem in RadGrid1.SelectedItems)
{
// do something
}
//Get the selected items in the MasterTableView
foreach (GridDataItem selectedDataItem in RadGrid1.MasterTableView.GetSelectedItems())
{
// do something
}
//Get the selected items in the MasterTableView including the selected items inside all nested tables
foreach (GridDataItem selectedDataItem in RadGrid1.MasterTableView.ChildSelectedItems)
{
// do something
}
}