Dynamic Gridview Bind

First You have to Drag Gridview Tool From your Toolbox
and place it to your design .aspx Page:-

Then at  the Code behind page in .cs file like Gridview.aspx.cs file
call  the following function to your button_click event:-

void BindData()
{
try
{
SqlConnection con = new SqlConnection(@"server=.\sqlexpress;
database=TestDB;Integrated Security=SSPI;");
 
DataSet ds = new DataSet();
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select id,name,flag 
from EmpData", con); 
 
da.Fill(ds);
BoundField BC1 = new BoundField();
BC1.HeaderText = "ID";
BC1.DataField = "id";
GridView1.Columns.Add(BC1);
BoundField BC2 = new BoundField();
BC2.HeaderText = "Name";
BC2.DataField = "name";
GridView1.Columns.Add(BC2); 
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
catch (Exception e)

{
Response.Write(e.Message.ToString());
con.Close();
}

Enjoy  :-)

Comments

Post a Comment

Popular posts from this blog

The Page Life Cycle of an ASP.NET and its controls.

Embed Tiny Editor in Your Web Application:

Introduction to .NET and .NET Architecture