Friday 26 October 2012

Bind gridview in asp.net


Source code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Bind data in Gridview in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Gridview ID="gridview1" runat="server" />
</div>
</form>
</body>
</html>

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
    Bindgridview();
}
}
protected void Bindgridview()
{
SqlConnection con = new SqlConnection(" your connection string");
SqlDataAdapter da = new SqlDataAdapter("Select * FROM TableName", con);
DataTable dt=new DataTable();
da.Fill(dt);
gridview1.datasource=dt;
gridview1.databind();
}
}

No comments:

Post a Comment