Due to features like column reorder, grouping, etc. it is not appropriate to pass indexes to the Cells collection of grid row (in order to reference cells in Telerik RadGrid rows). That is why each column in Telerik RadGrid has a UniqueName property of type string. This property is assigned automatically by the designer (For example, an autogenerated GridBoundColumn with DataField 'ContactName' would generate a UniqueName of 'ContactName'). You can also set it explicitly, although the automatic generation handles most cases.
Thus, if you previously reordered the columns in the grid, you will still be able to obtain the value from the respective column (through its UniqueName), although it actually has a new location in the grid hierarchy.
Accessing cells using column Unique Name
The main idea when retrieving values from grid items is to obtain reference to e.Item and e.Item.OwnerTableView.ParentItem (in hierarchical grid) in the ItemCreated/ItemDataBound/ItemComand/UpdateCommand/InsertCommand/DeleteCommand/etc. handler. Then you can get the cell values using column UniqueNames. When you want to access a cell within a grid item, you should use the following code to obtain the exact cell:
| VB.NET |
Copy Code |
|
Dim cell As TableCell = dataItem("ColumnUniqueName") |
| C# |
Copy Code |
|
TableCell cell = dataItem["ColumnUniqueName"]; |
and to access its Text property:
| VB.NET |
Copy Code |
|
dataItem("ColumnUniqueName").Text |
| C# |
Copy Code |
|
dataItem["ColumnUniqueName"].Text |
In case the grid item is in edit mode, the data extraction can be done through the corresponding column editor (depending on its type) either directly via the Controls collection of the column cell:
| VB.NET |
Copy Code |
|
CType(editedItem("ColumnUniqueName").Controls(0),TextBox).Text |
| C# |
Copy Code |
|
(editedItem["ColumnUniqueName"].Controls[0] as TextBox).Text |
or with
GridEditManager instance and its
GetColumnEditor(editableCol) method:
| VB.NET |
Copy Code |
|
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem) Dim editMan As GridEditManager = editedItem.EditManager ------------------------- Dim editor As IGridColumnEditor = editMan.GetColumnEditor(editableCol) |
| C# |
Copy Code |
|
GridEditableItem editedItem = e.Item as GridEditableItem; GridEditManager editMan = editedItem.EditManager; ------------------------- IGridColumnEditor editor = editMan.GetColumnEditor( editableCol ); |
A live example using the second approach with edit manager can be found
here.
The same approach can be applied for header or footer items. You just need to reference the header/footer item of the control and then pass again the column unique name to identify the cell of interest:
| C# |
Copy Code |
|
GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem; //fetch the data with headerItem["ColumnUniqueName"].Text or (headerItem["ColumnUniqueName"].Controls(0) as LinkButton).Text if sorting is enabled |
| VB.NET |
Copy Code |
|
Dim headerItem as GridHeaderItem = CType(RadGrid1.MasterTableView.GetItems(GridItemType.Header)(0), GridHeaderItem)
|
| C# |
Copy Code |
|
GridFooterItem footerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Footer)[0] as GridFooterItem; //fetch the data with footerItem["ColumnUniqueName"].Text |
| VB.NET |
Copy Code |
|
Dim footerItem as GridFooterItem = CType(RadGrid1.MasterTableView.GetItems(GridItemType.Footer)(0), GridFooterItem)
|
 |
The approach presented above will work for auto-generated columns or built-in Telerik RadGrid column types (except for template columns). Note that for template columns you have to find the control in the respective grid cell and extract its value. |
The indexer property (named
ItemIndex) is defined for objects of type
GridDataItem or
GridEditFormItem (or all descendants of
GridEditableItem class). In events related to creating, binding or for commands in items, the event argument has a property
Item to access the item that event is fired for. To get an instance of type
GridDataItem, use the following code:
| VB.NET |
Copy Code |
|
If(TypeOf e.Item GridDataItem) Then Dim gridDataItem As GridDataItem = e.Item End If
|
| C# |
Copy Code |
|
//presume e is the event argument object if (e.Item is GridDataItem) { GridDataItem gridDataItem = e.Item as GridDataItem; } |
Accessing cells in edit mode depending on Display/ReadOnly/Visible column properties
The code below shows how to access column cell value (for example for column "OrderID") on update/insert operation depending on the Display/ReadOnly/Visible properties of GridBoundColumn.
- if you set Display = False for OrderID column (displayed in Telerik RadGrid), you can access the new OrderID value for the edited grid item (entered by the user) on Update/Insert command using the following syntax:
| VB.NET |
Copy Code |
|
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem) string newValue = CType(editedItem("OrderID").Controls(0), TextBox).Text |
| C# |
Copy Code |
|
GridEditableItem editedItem = (GridEditableItem)e.Item; String newValue = (editedItem["OrderID"].Controls[0] as TextBox).Text; |
- if you set ReadOnly = True for OrderID column (displayed in Telerik RadGrid), you can access the value for the OrderID field in the edited grid item on Update/Insert command using a slightly different syntax:
| VB.NET |
Copy Code |
|
Dim editedItem As GridEditableItem = CType(e.Item, GridEditableItem) string newValue = editedItem("OrderID").Text |
| C# |
Copy Code |
|
GridEditableItem editedItem = (GridEditableItem)e.Item; String newValue = editedItem["OrderID"].Text; |
- if you set Visible = False for OrderID column (displayed in Telerik RadGrid), you will not be able to access these column values on update/insert.
Additional information concerning the differences between the Display/Visible/ReadOnly properties of GridBoundColumn you can find here.
Accessing Cells on client-side
You can access the cells client-side using GetCellByColumnUniqueName client-side method.
Accessing the DetailTables through NestedTableViews Collection
If you have a hierarchical grid, each item in GridTableView's Items collection has a child item of type GridNestedViewItem that has a set of DetailTables. So if you want to access, for example, the nested table view of the first item in the grid's master table, you should have the following code:
| C# |
Copy Code |
|
RadGrid1.MasterTableView.Items[0].ChildItem.NestedTableViews[0]; |
| VB.NET |
Copy Code |
|
RadGrid1.MasterTableView.Items(0).ChildItem.NestedTableViews(0) |
Or if you have a reference to an instance of an item in a child table and if you want to access the parent table view, you have to write the following code:
| C# |
Copy Code |
|
childItem.OwnerTableView.ParentItem.OwnerTableView; |
| VB.NET |
Copy Code |
|
childItem.OwnerTableView.ParentItem.OwnerTableView |
See Also