Tuesday, April 26, 2011

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.

No comments:

Post a Comment