March 10, 2009

How to refresh an ASP.NET GridView automatically at regular intervals




A common requirement is to refresh the GridView after regular intervals. ASP.NET contains the Timer control which comes in very handy to achieve such requirements. Here's how to do so:

Add the GridView and the Timer control inside an ASP.NET AJAX UpdatePanel as shown below :


<asp:UpdatePanel ID="UpdatePanel1" runat="server">


<ContentTemplate>


<asp:Timer ID="Timer1" runat="server" Interval="3600" ontick="Timer1_Tick"></asp:Timer>


<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="CustomerID"


DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True">


<Columns>


<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />


<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />


<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />


<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />


<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />


</Columns>


</asp:GridView>


</ContentTemplate>


</asp:UpdatePanel>




Then in the code behind, add the following code which refreshes the GridView after every minute

C#


protected void Timer1_Tick(object sender, EventArgs e)


{


GridView2.DataBind();


}




VB.NET


Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)


GridView2.DataBind()


End Sub




'Like' us on our FaceBook page if you find this blog useful. Thanks!


Did you like this post?
kick it on DotNetKicks.com Save on Delicious
subscribe via rss subscribe via e-mail
print this post follow me on twitter


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

8 Responses to "How to refresh an ASP.NET GridView automatically at regular intervals"
  1. Anonymous said...
    October 15, 2009 3:44 PM

    Thank you and best of luck with the book.

  2. Anonymous said...
    June 16, 2010 3:49 AM

    sank youu

  3. Rickson Menezes said...
    November 25, 2010 2:09 AM

    Gives me an error, Update Panel requires ScriptManager

  4. Rickson Menezes said...
    November 25, 2010 2:25 AM

    I added a script Manager tag just above the Update Panel and it worked. It was updating my gridview every x seconds

  5. Anonymous said...
    December 12, 2010 6:07 PM

    Hello, im getting a PageRequestManagerParseErrorException, i have the code just like you suggest, what might be going wrong?

    Thank you.

  6. Suprotim Agarwal said...
    December 12, 2010 7:07 PM

    For the PageRequestManagerParseErrorException error, try this link

    http://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how-to-avoid-it.aspx

  7. Anonymous said...
    April 5, 2011 5:08 AM

    some of the function within the grid not work??

  8. Rahul Ostwal said...
    October 5, 2011 3:16 AM

    I am filling data after every 5 min. but by this code first time it take 5 min. to fill grid. how to avoid this?

 

Copyright © 2009-2012 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions