RadGrid for ASP.NET AJAX

RadControls for ASP.NET AJAX

1. Incorrect structure of columns or DetailTables.

In order for RadGrid to work properly, it’s very important to build the grid structure correctly. Generally speaking, when the developer creates the structure using the designer (in VS.NET) there is less of a chance that mistakes will be made. The more difficult and error-prone task is creating the structure programmatically. Since RadGrid saves all its structure properties (DetailTables, Columns, etc.) into the ViewState, building a RadGrid dynamically is a task very similar to creating and adding controls dynamically to a web-page.

See http://weblogs.asp.net/infinitiesloop/archive/2006/08/25/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_.aspx

There are two possible approaches to dynamic RadGrid creation that you can use in order to be sure that RadGrid will behave normally.

1st Scenario:

Create the RadGrid instance and the grid structure in the Page.Init event handler. Then the instance of RadGrid is added to the controls collection of the Page. In this case, no ViewState is required for grid structure to be persisted as it is recreated on each page initialization. There are no other special requirements in this case.

2nd Scenario

Add RadGrid to the page using the designer, but create the structure programmatically in the Page.Load event handler. You should remember that the structure should be built only in case where Page.IsPostBack is false. You should also add the instances of any created objects, such as columns and detail tables, to the RadGrid before any of those object’s properties have been set. This is important because no ViewState is managed for the object before it has been added to the corresponding collection.

Example:

2. Misusing or not using NeedDataSource event

The NeedDataSource event helps developers easily control scenarios like paging, sorting, and grouping, with Telerik RadGrid . Using these types of PostBack events, which RadGrid fires, can lead to a change in the Items collections of each GridTableView in a Telerik RadGrid . A structural change here means that items should be recreated. In order to achieve that, the Telerik RadGrid should have a DataSource assigned and the DataBind() method should be called. Then, instead of writing all the code to handle the appropriate scenario, the developer can just let Telerik RadGrid handle these changes internally and only handle NeedDataSource which fires at the exact time the items should be recreated.

Important

Developers should avoid using NeedDataSource event handler to change the structure of an instance of RadGrid. If it is necessary to change the structure, you may need to use the event argument object to check the reason that NeedDataSource was being fired. Also the developer should not call the DataBind() method in the NeedDataSource event handler. It will be called internally when needed.

Often developers do not realize that NeedDataSource is called only when Telerik RadGrid "knows" about the structural changes. If a sort or page command executed, for example. In all other cases, when developer has made changes to the structure of the grid that require binding, the developer should call the Rebind() method. This method will first check if the DataSource has been assigned, then it will force the RadGrid instance to fire NeedDataSource and then DataBind(). More information about using NeedDataSource and when this event fires can found in the online demo concerning this subject.

3. Using EnableViewState = false in non-supported scenarios

See Switching off RadGrid's viewstate topic for details about the EnableViewState property.

4. Using cell numeric index to find a cell in a GridItem instead of using item's string index by Column.UniqueName

One of the major differences between RadGrid and the standard .NET DataGrid control is the dynamic column structure of RadGrid. Unlike the DataGrid, RadGrid supports operations such as column reordering and grouping which alter the Cells collection of GridItem objects in a way that can’t be predicted by the page developer. For example, when using a DataGrid, many developers used to search a cell in an item using the following code:

But imagine that the user has changed the column order using client-side drag/drop. Then the cell with index 4 will no longer refer the same field value. That's why RadGrid provides the ability to access cells in items using the corresponding column’s UniqueName. For example, if you have a column with the unique name "CustomerID" you can find the corresponding cell in a GridItem using:

This will prevent you from accessing the wrong cell.

5. Finding controls inside RadGrid

You should note the following about searching for controls in an item that is in edit mode. Unlike the DataGrid/GridView control, RadGrid supports EditForms feature which is set by default. It alters the "traditional" editing style by displaying an edit form item (row), below the item currently being edited, instead displaying the in-place editors. That's why, if you have template columns for example, and you have to search for a control that is in the edit template, you should search the EditFormItem instead of the edited item. This item is accessible using the GridDataItem.EditFormItem property.

Additional details can be found in these help resources:

InPlace editing

EditForms editing

Custom edit forms

6. Overusing hierarchical structure in treeview style scenarios

To better understand how Telerik RadGrid deals with hierarchy, refer to the section: Understanding hierarchical grid structure.

Telerik RadGrid supports hierarchical database structure or building hierarchy through self-referencing source table (having ID->ParentID relations in the same DataTable). This means that with standard hierarchy in each level of hierarchy all items will have an equal number of details tables (one or several in the same level).

This is unlike the Telerik RadTreeView control which supports different number of sub-items on each level - the equivalent in this case is self-referencing hierarchy which allows you to have variable number of nested sub-tables in each level. Refer to this online demo for further reference.

Note

Note that RadGrid will not behave correctly if you add detail tables programmatically in DetailTableDataBind event handler.

7. When handling the ItemDataBound or ItemCreated events, forgetting to check for the appropriate:

  • GridItemType - this topic from the documentation which discusses this matter thoroughly.
  • Detail table in the hierarchical structure - This check can be done using:

    - e.Item.OwnerTableView.DataMember property (.NET 2.x when not using data source controls)

    - e.Item.OwnerTableView.DataSourceID (.NET 2.x/3.x with data source controls) or

    -e.Item.OwnerTableView.Name (.NET 2.x/3.x)

    where e is the event parameter of the event handler method. This way you can avoid possible problems when performing customization of items specific to a certain level of the hierarchy.

    A code sample is available in this section of the help as well.