Showing posts with label DataSource. Show all posts
Showing posts with label DataSource. 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 )

Databiding a GridView

 A gridview can 'databind' to any collection/list of objects.

Properties of note:
DataSource - the collection to bind to
DataKeyNames - an array of the field on the datasource to use as the key/identifier (often the Primary Key, in the case of database results). Optional.
Databind - causes the Gridview to bind with the datasource - starts executing other events such as RowDataBound, etc.

Dim myModel As New ModelFacade

gvShoppingCart.DataSource = myModel.getCart()
gvShoppingCart.DataKeyNames = New String() {"CartID"}
gvShoppingCart.DataBind()

In the example above, the getCart method of the myModel object, returns a list. The CartID feild is used as the DataKeyName.