Hello Friends....
Welcome to this Blog...This Blog is specially for the .Net Developers where all developers can discuss the .Net and Sql Articles.
"And will you succeed? Yes! You will indeed!(98 and 3/4 percent guaranteed)."

Friday, September 3, 2010

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  :-)

1 comment: