Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Automatically Referesh A GridView

Article:
Viewed:  1183 
Posted On:  02/09/2012 22:40:40 
How we can make a GridView referesh automatically.. 

In this article I am going to show how we can refresh a GridView after a particular time automatically.

This is my aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Auto Referesh Grid View</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="0" cellspacing="0" width="50%" align="center">
            <tr>
                <td>
                    <asp:ScriptManager ID="ScriptManager1" runat="server" />
                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:Timer ID="AutoRefreshTimer" runat="server" Interval="1000" OnTick="AutoRefreshTimer_Tick" />
                            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%">
                                <Columns>
                                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                                    <asp:BoundField DataField="LastName" HeaderText="LastName" />
                                    <asp:BoundField DataField="JoiningDate" HeaderText="Joining Date" />
                                </Columns>
                            </asp:GridView>
                            <asp:Label ID="lblMsg" runat="server" />
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html> 

 

This is my aspx.cs code

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    } 

    private void BindData()
    {
        SqlConnection con = new SqlConnection("Server=.;Database=MyData;Uid=sa; pwd=wintellect");
        SqlCommand cmd = new SqlCommand("SELECT *  FROM Employee", con);
        try
        {
            cmd.Connection.Open();
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            cmd.Connection.Close();
            cmd.Connection.Dispose();
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }

    protected void AutoRefreshTimer_Tick(object sender, EventArgs e)
    {
        BindData();
    }
} 

When I run the applciation

AutoReferesh1.png

Figure 1. 

If user add a new row in this table then it refresh automatically ..

AutoReferesh2.png 

Figure 2.
 

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us