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


20 comments:

  1. Thank you and best of luck with the book.

    ReplyDelete
  2. Gives me an error, Update Panel requires ScriptManager

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

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

    Thank you.

    ReplyDelete
  5. 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

    ReplyDelete
  6. some of the function within the grid not work??

    ReplyDelete
  7. 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?

    ReplyDelete
  8. Hi Rahul.
    You just have to fire the Databinder code in PageLoad, like this:

    protected void Page_Load(object sender, EventArgs e)

    {

    GridView2.DataBind();

    }

    ReplyDelete
  9. LO MAS GRANDE MUCHAS GRACIAS!!!!!!!!!!!!!!!!!!!!!!!!!

    FCP

    ReplyDelete
  10. Simply gr8.........

    ReplyDelete
  11. Thanks for great article!!!!What we can do if we have 2 datagridviews and we want update every 5 seconds with different select commands both???

    ReplyDelete
  12. Thanks for this article. It was very beneficial for us.

    ReplyDelete
  13. I know I'm quite late to this comment thread but I'm a bit green when it comes to ASPX gridview and I could use some feedback.

    I have three gridviews with different sql query results being displayed that have paginated results. These gridview forms will be displayed on a monitor for an entire department and will need to automatically iterate through the paged results every two to three minutes continuously.

    While I am familiar enough with PHP to make this happen. I'm utterly lost when it comes to making this happen in aspx. Can you offer some very simple and straight forward feedback on how I might get this working?

    ReplyDelete
  14. I have successfully added the GridView and Timer control inside my ASPX.NET page and have updated the associated VB.NET file with the code noted. I also ran into the ScriptManager issue which I've added successfully. However, the gridview is not paging automatically.

    Is the C# code a requirement? I am extremely new to this, so thanks in advance for your patience with my question.

    ReplyDelete
  15. Thanks so much for responding. I was finally able to get one of the gridviews looping through the pages results on a timer. :-)

    ReplyDelete
  16. hello i have this problem not refresh data when using binding propieties

    i load data with behind method in vb .net the first time load teh data but in the second call not display data

    ASPxGridView1.DataSource = Nothing
    ASPxGridView1.DataSource = dtReporteGeneral
    ASPxGridView1.UpdateEdit()


    ASPxGridView1.DataBind()

    updtReporteGeneral.Update()
    diseno_grdidev()

    and display next error

    ---------------------------

    ---------------------------
    error: Referencia a objeto no establecida como instancia de un objeto.
    ---------------------------

    ---------------------------
    not load columns or any data in the grid.

    pleas help me

    ReplyDelete