New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Most Common Mistakes

This help article describes seven common mistakes that customers make using RadGrid and offers solutions to the problems.

1. Incorrect Structure of Columns or DetailTables.

In order for RadGrid to work properly, it is very important to build the grid structure correctly. When you create the structure using the designer (in Visual Studio) 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 of its structure properties (DetailTables, Columns, etc.) into the ViewState, building a RadGriddynamically 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

The following two scenarios offer possible approaches to dynamically create a RadGrid that ensure thatRadGrid will behave normally.

1st Scenario:

You can 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 ViewStateis required for the grid structure to be persisted because it is recreated on each page initialization. There are no other special requirements in this case.

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);
....
//Add to page controls collection
this.PlaceHolder1.Controls.Add( RadGrid1 );          
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

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

Example:

private void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        GridBoundColumn boundColumn;
        //Important: first Add column to the collection
        boundColumn = new GridBoundColumn();
        this.RadGrid1.MasterTableView.Columns.Add(boundColumn);
        //Then set properties
        boundColumn.DataField = "CustomerID";
        boundColumn.HeaderText = "CustomerID";
    }
}
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
        Dim column1 As New GridBoundColumn
        'Important: first Add column to the collection
        Me.RadGrid1.MasterTableView.Columns.Add(column1)
        'Then set properties
        column1.DataField = "CustomerID"
        column1.HeaderText = "CustomerID"
    End If
End Sub

2. Misusing or Not Using NeedDataSource Event

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

You should avoid using the 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 reasonthat NeedDataSource was being fired. Also, the developer should not call the DataBind() method inthe NeedDataSource event handler. It will be called internally when needed.

Often developers do not realize that NeedDataSource is called only when RadGrid "knows" about the structural changes, such as when a sort or page command is executed. In all other cases, when you make changes to the structure of the grid that require binding, you should call the Rebind() method. This method will first check if theDataSource has been assigned, then it will force the RadGrid instance to fire NeedDataSourceand then DataBind(). You can find more information about using NeedDataSource and when this event fires in this online demo

3. Using EnableViewState = false in Non-supported Scenarios

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

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 GridItemobjects 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:

item.Cells[4]          
item.Cells(4)

However, imagine that the user has changed the column order using a client-side drag-and-drop operation. 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:

item.Cells["CustomerID"] 
//for GridDataItem you can replace that with
item["CustomerID"]          
item.Cells("CustomerID")
'for GridDataItem you can replace that with
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/GridViewcontrol, RadGrid supports the EditForms feature that 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 is 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 theEditFormItem instead of the edited item. This item is accessible using theGridDataItem.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 RadGrid deals with hierarchy, refer to the article: Understanding hierarchical grid structure.

RadGridsupports 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 orseveral in the same level).

This is unlike the RadTreeView control which supports different number of sub-items on each level - the equivalent in thiscase is RadTreeList control that allows you to have variable number of nested sub-tables in each level. Refer tothis online demo for further reference.

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

7. Common Mistakes When Handling the ItemDataBound or ItemCreated Events.

ItemDataBound and ItemCreated event handler allows you to manipulate the controls and the values of each cell in RadGrid. The differences between those two events is discussed thoroughly in this topic. When handling ItemCreated and ItemDataBound events, forgetting to check for the appropriate:

  • GridItemType - At the following articleyou can find a list with all available item types.

  • 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/4.x with data source controls) or-e.Item.OwnerTableView.Name (.NET 2.x/3.x/4.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.

See Also

In this article