Showing posts with label Databound. Show all posts
Showing posts with label Databound. Show all posts

Tuesday, April 26, 2011

Gridview DataBound Event

This event fires once the Gridview has finished Databinding - that is, the Gridview has been populated with all of the data on the Datasource.

Don't get it mixed up with RowDataBound event which fires after each row has finished binding.

This is a great place to run any code that needs to wait until the Gridview is finished binding - perhaps we have been accumulating the values in each row, as they bind & wish to write them out at the end.

"BaseDataBoundControl.DataBound Event (System.Web.UI.WebControls)" ( http://bit.ly/gXWo3S )

Finding controls in the GridView

Because a GridView renders out multiple rows, each of them contain controls with identical names. Finding the right control requires a trick.

For instance, in the RowDataBound/DataBound event, if we were looking for a particular label so that we could write a value to it 'on the fly', we would use the following code:

Dim lblItemotal As Label = CType(e.Row.FindControl("lblItemTotal"), Label)

The CTYPE function is a conversion function. In this case, it will return all of the data associated with the label we are trying to locate, as a Label object. Once located, we can manipulate this particular instance.