Most Common RadGrid Mistakes
This article describes seven common mistakes when using RadGrid and explains how to avoid them.
1. Build the Columns and Detail Tables Correctly
Build the RadGrid structure consistently so that its columns and detail tables are recreated at the correct point in the page lifecycle. Creating the structure with the Visual Studio designer reduces the chance of configuration errors. When you create it programmatically, remember that RadGrid stores structure properties such as DetailTables and Columns in view state.
The following approaches are supported for creating a grid structure programmatically.
Build the Structure in Page.Init
Create the RadGrid instance and its structure in the Page.Init event handler, then add the instance to the page's control collection. Because the structure is recreated during every page initialization, view state is not required to persist the structure.
Create a RadGrid and its columns during Page.Init
this.RadGrid1 = new RadGrid();
this.RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(this.RadGrid1_NeedDataSource);
this.RadGrid1.DetailTableDataBind += new GridDetailTableDataBindEventHandler(this.RadGrid1_DetailTableDataBind);
this.RadGrid1.CssClass = "RadGrid";
this.RadGrid1.Width = Unit.Percentage(100);
this.RadGrid1.PageSize = 5;
this.RadGrid1.AllowPaging = true;
this.RadGrid1.AutoGenerateColumns = false;
this.RadGrid1.GroupingEnabled = true;
this.RadGrid1.ShowGroupPanel = true;
this.RadGrid1.ClientSettings.AllowDragToGroup = true;
this.RadGrid1.MasterTableView.DataMember = "Customers";
this.RadGrid1.MasterTableView.PageSize = 15;
GridBoundColumn boundColumn = new GridBoundColumn();
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
// Add other columns and detail tables here.
this.PlaceHolder1.Controls.Add(this.RadGrid1);Build the Structure in Page.Load
Add RadGrid to the page with the designer, but create its programmatic structure in the Page.Load event handler only when Page.IsPostBack is false. Add each new column or detail table to the corresponding collection before setting its properties so that view state can manage the object.
Add a dynamically created column to the grid before setting its properties
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
GridBoundColumn boundColumn = new GridBoundColumn();
this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
}
}2. Use the NeedDataSource Event Correctly
The NeedDataSource event lets RadGrid recreate its items after operations such as paging, sorting, and grouping. Assign a DataSource in this event and let RadGrid call DataBind() internally. Do not call DataBind() in the NeedDataSource handler.
Avoid changing the RadGrid structure in the NeedDataSource event handler. If a structural change is necessary, use the event argument to check why NeedDataSource was raised. Do not call DataBind() in the handler because RadGrid calls it internally when needed.
RadGrid raises NeedDataSource when it knows that an operation requires its items to be recreated. After changing the grid structure or data outside those operations, call Rebind(). This method causes RadGrid to raise NeedDataSource and bind the data again. For an example, see the NeedDataSource demo.
3. Disable ViewState Only in Supported Scenarios
See Optimizing ViewState usage for details about the EnableViewState property.
4. Access Cells by UniqueName Instead of a Numeric Index
Unlike the standard .NET DataGrid, RadGrid has a dynamic column structure. Column reordering and grouping can change the Cells collection of GridItem objects, so a numeric index can refer to a different field after a user action.
Do not use a numeric cell index such as item.Cells[4] because column reordering can change which field the index identifies.
Access a GridDataItem cell by its column UniqueName
GridDataItem dataItem = (GridDataItem)item;
dataItem["CustomerID"];Using the column's UniqueName keeps the lookup associated with the intended field.
5. Find Controls in the Correct Edit Item
Unlike the DataGrid or GridView controls, RadGrid uses the EditForms mode by default. The edit form appears in a separate item below the item being edited instead of replacing the row with in-place editors. Search the EditFormItem when you need to find a control from an edit template. Access it through the GridDataItem.EditFormItem property.
Additional details can be found in these help resources:
6. Choose the Appropriate Hierarchical Control
For an overview of RadGrid hierarchy, see Understanding hierarchical grid structure.
RadGrid supports a hierarchical database structure or a self-referencing source table with ID and ParentID relations. In a standard hierarchy, each level has the same number of detail tables for its items. A level can have several tables when the data model requires them.
Unlike RadTreeView, RadGrid does not support a different number of nested tables for each item in the same hierarchy level. Use RadTreeList when you need variable-depth, tree-like data. See the RadTreeList overview demo.
Note that RadGrid will not behave correctly if you add detail tables programmatically in DetailTableDataBind event handler.
7. Check the Item Type and Table When Handling Item Events
The ItemCreated and ItemDataBound event handlers let you manipulate controls and cell values in RadGrid. For event differences, see Differences between ItemCreated and ItemDataBound.
When handling either event, check the following values before applying logic:
- GridItemType: Check
e.Item.ItemTypebefore casting or customizing the item. See the GridItemType API reference for the available values. - Detail table: Check
e.Item.OwnerTableView.DataMember,e.Item.OwnerTableView.DataSourceID, ore.Item.OwnerTableView.Nameto identify the hierarchy level. The appropriate property depends on how the table is bound.
See Distinguishing grid rows in ItemCreated and ItemDataBound for an example.