.NET Framework Class Library
GridView..::.OnRowDataBound Method

Raises the RowDataBound event.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
Protected Overridable Sub OnRowDataBound ( _
    e As GridViewRowEventArgs _
)
Visual Basic (Usage)
Dim e As GridViewRowEventArgs

Me.OnRowDataBound(e)
C#
protected virtual void OnRowDataBound(
    GridViewRowEventArgs e
)
Visual C++
protected:
virtual void OnRowDataBound(
    GridViewRowEventArgs^ e
)
JScript
protected function OnRowDataBound(
    e : GridViewRowEventArgs
)
Remarks

Before the GridView control can be rendered, each row in the control must be bound to a record in the data source. The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnRowDataBound method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:

When overriding OnRowDataBound in a derived class, be sure to call the base class's OnRowDataBound method so that registered delegates receive the event.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Community Content

boaldave
I experienced an issue with ViewState that was resolved using Gridview_RowDataBound

My experience was as follows:

There is an important critical difference between Gridview_RowCreated, and Gridview_RowDataBound events.


If you want to store ViewState for your Templated Controls in GridView rows, you must do it in Gridview_RowDataBound!!!!


If you populate the Templated Control values using Gridview_RowCreated, the page is rendered as expected, however, ViewState associated with those Templated Controls IS NOT SAVED!!!


If you populate the Templated Control values using Gridview_RowDataBound, the page is rendered as expected, AND ViewState associated with those Templated Controls IS SAVED!!!


Here is the problem this caused me.


I have a button and a gridview on a page. The button does something useful, but does not cause databinding on the GridView. I populated a hyperlink in the Gridview_RowCreated event, based on values casted from the e.Row.DataItem object. When the first Page_Load occurs, the Page_Load Event fires, the GridView.DataBind() is called, which causes first the Gridview_RowCreated event to fire for the 1st row, then the Gridview_RowDataBound Event is fired for the 1st row, then this sequence is repeated for each row. Please note that the data is already data bound to the Row when the Gridview_RowCreated event fires and you can tell because the e.Row.DataItem is not null. So on the first Page_Load, you can populate your Templated Controls with with data values.


Now here where the problem occurs. When the Postback occurs, i.e., when the button is clicked, the Gridview_RowCreated event actually fires before Page_Load and when it does, the e.Row.DataItem is null. Only the Gridview_RowCreated event fires, not the Gridview_RowDataBound Event, because the GridView is not being bound at this moment. So if you were depending on the e.Row.DataItem object, to set Templated Control values, it is not going to be there. Please also note that since you populated the Templated Control values in the Gridview_RowCreated event on the first Page_Load event, you have no ViewState because it WAS NOT SAVED, so the values that you set your Templated Controls to on the first Page_Load event, will NOT be loaded when the Page Loads the ViewState. If you had populated the Templated Control values in the Gridview_RowDataBound Event, the ViewState would have been saved, and the Templated controls would be populated with the values they were populated with on the first Page_Load.

So the moral of the story is be Gridview_RowDataBound causes ViewState to be saved!


Page view tracker