Dynamic GridView


Adding Columns in Gridview Dynamically.

Adding Columns in Gridview Dynamically.


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();
}

}


Comments

Author: Pal (Parthiv) Patel14 Aug 2010 Member Level: Gold   Points : 0

NICE CODE BUDDY

KEEP IT UP

POST GOOD POST AND EARN MONEY

Author: Siva Prasad25 Mar 2012 Member Level: Gold   Points : 0

Yeah, We can add columns dynamically in grid view. Nice code friend. I hope you don't mind. Finally block is missing to release unused objects.

Guest Author: Dhruva05 Apr 2012

Good one

Guest Author: vinita shakyawar28 Jun 2012

plz send source code for this dynamic gridview to my email id:vinita.shakyawar@rediffmail.com

Author: Atul Sharma05 Oct 2012 Member Level: Silver   Points : 0

i want in gridview that export data into excel sheet..can u give me code...