GridView DataBinding Events

This is a well commented example for a GridView with implementation of RowDataBound and DataBound events. I also demonstrate in it some of the important properties of GridView, like the slight differences when dealing with Grid Paging, Rows, Cells, and Columns. I prefered to heavily comment the code than to write separate paragraphs describing it as I do believe that the code is what gets the idea in a direct way.

The example is a simple page that uses SqlDataSource to connect to a SQL Server 2005 Express database and show the results on a GridView with paging capability.


Many parts of the code don’t show the best practices for their situation and those are only included for demonstration purpose as they are not the main focus of the example.

Let’s start with the code behind of the page:

As it’s the most important part

Now to the page XHTML source:

Samples of the output of the code:

Hope that you like it.

Share With Friends:

P.S. Please help me out by checking this offer, then look below for a small Thank You.

How did I learn that?

As a bonus for coming here, I'm giving away a free newsletter for web developers that you can sign up for from here.
It's not an anything-and-everything link list. It's thoughtfully collected picks of articles and tools, that focus on Angular 2, ASP.NET 5, and other fullstack developer goodies.

  • Congrats on the new blog, so are you giving up the geeks with blogs one?

  • I’m using cross posting right now until I settle on something! I’m worried that this blog might be more restricted in topics than GWB. I talked to the owners of both, and still not having a final decision yet!!

  • Wishing you the best,
    I subscribed for both now so keep the good posts coming.

  • It’s easy, Amr :). Thanks to feedburner!

    You can subscribe to
    This feed now points to my old blog, but, everything I’ll post here is automatically cross posted there. Eventually, I’ll make it loaded from this new blog. I’ll do after I reach twenty-something posts, God Willing.
    So, just subscribe to that, and you’ll not even realize the change when it takes place – I hope.

    thank you for caring anyway.

  • http://

    It’t cool, but what if I use database paging and the ObjectDatasource. When I test this situation, the result is that the e.AffectedRows equal -1 in the ObjectDataSource’s OnSelected event handle.
    the Selecting method is List GetMessages(),and the SQL for selecting is:
    SELECT * FROM (SELECT MessageID,[From],Subject,CreatedDate,IsRead,IsReply,ReturnReceipt,FolderID,ROW_NUMBER() OVER ( ORDER BY CreatedDate DESC) AS RowNum FROM Mail_MailMessages WHERE FolderID=@FolderID )Messages
    WHERE Messages.RowNum BETWEEN (@PageIndex*@PageSize+1) AND ((@PageIndex+1)*@PageSize) ORDER BY CreatedDate

  • http://

    how would you place two table data in one gridview

  • http://

    really its good ………………………………..

  • http://

    Hi! It was really awesome as I’m a beginner to .Net but I could easily understand the GridView DataBinding. Thanks once again Mohammed…

  • http://

    The article is good.

  • http://

    pls write ur code in VB.Net also.So that the readers will have both knowledge as well as persons who dnt any one can get knowledge abt the code which they require.

  • hello.

    thank you for this article.I dont know in vs2008 instead of ItemDataBound Ican use which one of Event??you know in visual studio 2008 we dont have ItemDataBound.

    I ask you Please help me.

  • http://


  • Foo

    You forgot to actually include an example of the actual DataBinding event. Article should have been titled what to do after the data is bound.

  • Pingback: Databinding gridview | Testicleradio()

  • kettic
  • Guest

    The C# GridView databinding guide shows databinding to a single level of data or multiple levels of hierarchical data.

  • Pingback: nba 2k16 mt for sale ps4()

  • Pingback: cheap csgo keys()

  • Pingback: free my team coins()