Gridview Data Binding

Posted on 2013-10-11
Medium Priority
Last Modified: 2013-10-11
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.
  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.
  End Sub

  Protected Sub TaskGridView_RowCancelingEdit()
    'Reset the edit index.
    TaskGridView.EditIndex = -1
    'Bind data to the GridView control.
  End Sub
Question by:rckrch
  • 2
LVL 52

Expert Comment

by:Carl Tawn
ID: 39565584
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 :)

Author Comment

ID: 39565717
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!!
LVL 52

Accepted Solution

Carl Tawn earned 1300 total points
ID: 39565858
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.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question