C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Adding dynamic columns in gridview


Posted Date: 14 Dec 2008    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: DharmarajMember Level: Diamond    
Rating: 1 out of 5Points: 10



This code will explain how to dynamically create columns in gridview.
Create a webform and add a gridview. In the code behind part add the following code.


protected void Page_Load(object sender, EventArgs e)
{
loadDynamicGrid();
}

private void loadDynamicGrid()
{
#region Code for filling datatable
//instance of datatable
DataTable dt = new DataTable();
//instance of datacolumn
DataColumn dc = new DataColumn("ID",typeof(System.Int32));
dc.AutoIncrement = true;
dt.Columns.Add(dc);
dc = new DataColumn("NAME", typeof(System.String));
dt.Columns.Add(dc);
for (int i = 0; i < 5; i++)
{
//creating instance for datarow
DataRow dr = dt.NewRow ();
dr["NAME"] = "Dharma";
dr["ID"] = i;
dt.Rows.Add(dr);
}
#endregion
//loop through columns create boundfield dynamically
foreach (DataColumn col in dt.Columns)
{
BoundField bfield = new BoundField();
bfield.DataField = dc.ColumnName;
bfield.HeaderText = dc.ColumnName;
dngrview.Columns.Add(bfield);
}
dngrview.DataSource = dt;
dngrview.DataBind();
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Dynamic columns in gridview  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: ASPNET DATAGRID with styles
Previous Resource: Export the content of datagrid to excel sheet
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use