Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

More...

Resources » Code Snippets » ASP.NET GridView

Dynamically adding columns to the GridView control


Posted Date:     Category: ASP.NET GridView    
Author: Member Level: Silver    Points: 8



 


With this code sample we can add columns to the GridView control at run time in ASP.NET. To successfully do so, follow the steps


1)define the column.


BoundField ID=NULL;
ID=new BoundField();
ID.HeaderText="ID";
ID.DataField="ID";
GridView1.Columns.Add(ID);

BoundField City=NULL;
City=new BoundField();
City.HeaderText="City";
City.DataField="City";
GridView1.Columns.Add(City);



2)define the datatable column


DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("ID",typeof(string)));
dt.Columns.Add(new DataColumn("City",typeof(string)));



3)define the DataRow


DataRow dr=dt.NewRow();
dr["ID"]="";
dr["City"]="";



4)Add DataRow into DataTable.


dt.Rows.Add(dr);



5)apply DataTable (dt) to the DataSource Property of GridView1


GridView1.DataSource=dt;



6) bind GridView


GridView1.DataBind();





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


Responses to "Dynamically adding columns to the GridView control"

No responses found. Be the first to respond...

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.


    Next Resource: To add a hyperlink field in grid view
    Previous Resource: Editing, Deleting Gridview in VB.net 2.0
    Return to Resources
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    GridView  .  Adding Columns to GridView  .  Dynamically Adding Columns to GridView  .  Adding Columns to GridView in ASP.NET  .  Adding Columns to GridView control  .  Adding Columns Dynamically to GridView  .  
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.