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
rckrchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
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 :)
0
rckrchAuthor Commented:
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!!
0
Carl TawnSystems and Integration DeveloperCommented:
It depends how and when you are changing it. If you change it client-side using javascript then it doesn't matter. If you are doing it in the RowDataBound event, for example, then it is part of the binding process already.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.