Link to home
Start Free TrialLog in
Avatar of rckrch
rckrchFlag for United States of America

asked on

Gridview Data Binding

I have a basic question about data binding to a gridview.  Below is a code snipet from the MSDN website.  The questions I have is: Why do you need to perform the databind() function if you are not changing the datasource that is bound to the control?

If I have the Gridview DataSourceID="..." property declared in the asp.net code then, unless I am changing the datasource I should never have to perform another databind in code behind - is this correct?

Changing the attribute (appearance - selected row back color, rowstate, etc) of the gridview control is not affected by databinding, correct?

Thanks in advance.



Protected Sub TaskGridView_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    TaskGridView.PageIndex = e.NewPageIndex
    'Bind data to the GridView control.
    BindData()
  End Sub

  Protected Sub TaskGridView_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    'Set the edit index.
    TaskGridView.EditIndex = e.NewEditIndex
    'Bind data to the GridView control.
    BindData()
  End Sub

  Protected Sub TaskGridView_RowCancelingEdit()
    'Reset the edit index.
    TaskGridView.EditIndex = -1
    'Bind data to the GridView control.
    BindData()
  End Sub
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

You have to call DataBind() because the context of the data being displayed is changing. The GridView is only showing a snapshot of the data from the datasource, and what it displays will change based on the context.

So, what it displays in display mode may be different from what is displayed in edit mode. Similarly when paging - the grid is showing data from a particular set of rows in the datasource - if the page being displayed changes then you need to rebind the grid to the datasource so that it can draw itself based on the page of data being requested.

Hope that makes some sense :)
Avatar of rckrch

ASKER

Thanks carl_tawn fo the reply.  When I change the backcolor of a gridview row what context is changing with respect to the data it is bound to?

Thanks very much for the reply!!
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial