Resources » Code Snippets » ASP.NET GridView

Dynamic GridView


Posted Date: 13-Aug-2010  Last Updated:   Category: ASP.NET GridView    
Author: Member Level: Gold    Points: 7


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

}


Did you like this resource? Share it with your friends and show your love!

Responses to "Dynamic GridView"
Author: Pal (Parthiv) Patel    14 Aug 2010Member Level: Gold   Points : 0
NICE CODE BUDDY

KEEP IT UP

POST GOOD POST AND EARN MONEY



Author: Siva Prasad    25 Mar 2012Member 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: Dhruva     05 Apr 2012
Good one


Guest Author: vinita shakyawar     28 Jun 2012
plz send source code for this dynamic gridview to my email id:vinita.shakyawar@rediffmail.com


Author: Atul Sharma    05 Oct 2012Member Level: Silver   Points : 0
i want in gridview that export data into excel sheet..can u give me code...


Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    poonam
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India