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.
CopyC#
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;
boundColumn = new GridBoundColumn();
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
....
this.PlaceHolder1.Controls.Add( RadGrid1 );
CopyVB.NET
Me.RadGrid1 = new RadGrid();
AddHandler Me.RadGrid1.NeedDataSource, New GridNeedDataSourceEventHandler(AddressOf Me.RadGrid1_NeedDataSource)
AddHandler Me.RadGrid1.DetailTableDataBind, New GridDetailTableDataBindEventHandler(AddressOf Me.RadGrid1_DetailTableDataBind)
Me.RadGrid1.CssClass = "RadGrid"
Me.RadGrid1.Width = Unit.Percentage(100)
Me.RadGrid1.PageSize = 5
Me.RadGrid1.AllowPaging = true
Me.RadGrid1.AutoGenerateColumns = false
Me.RadGrid1.GroupingEnabled = true
Me.RadGrid1.ShowGroupPanel = true
Me.RadGrid1.ClientSettings.AllowDragToGroup = true
Me.RadGrid1.MasterTableView.DataMember = "Customers"
Me.RadGrid1.MasterTableView.PageSize = 15
Dim column1 As New GridBoundColumn
boundColumn.DataField = "CustomerID"
boundColumn.HeaderText = "CustomerID"
Me.RadGrid1.MasterTableView.Columns.Add(boundColumn)
‘Add to page controls collection
Me.PlaceHolder1.Controls.Add( RadGrid1 )</pre>
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:
CopyC#
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
GridBoundColumn boundColumn;
boundColumn = new GridBoundColumn();
this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = "CustomerID";
boundColumn.HeaderText = "CustomerID";
}
}
CopyVB.NET
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Dim column1 As New GridBoundColumn
Me.RadGrid1.MasterTableView.Columns.Add(column1)
column1.DataField = "CustomerID"
column1.HeaderText = "CustomerID"
End If
End Sub
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
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:
CopyC#
CopyVB.NET
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:
CopyC#
item.Cells["CustomerID"]
item["CustomerID"]
CopyVB.NET
item.Cells("CustomerID")
item("CustomerID")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 editingEditForms editingCustom 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.