Asp.Net News
Free Tech Magazines
Asp.Net Web Hosting
Suggest Us
Link to Us
Feeds Subscription
Questions & Answers
Tips & Tricks
 Grid View
Posted by Shashikala on  12/30/2010 1:21:50 AM Category: ADO.NET
Add to my favorites
Email to friend
How to generate Columns in grid view dynamiclly at run time?

Post Your Reply Post New Question

 Viewer's Reply
Create Dynamic Columns for GridView control in Asp.Net
Answered By Moderator1 on 1/16/2011 2:07:16 AM
Below code helps you to create dynamic GridView columns.

BoundField CusCode = new BoundField();
BoundField CusName = new BoundField();
BoundField CusAge = new BoundField();
BoundField CusEmail = new BoundField();
BoundField SomeDecimals = new BoundField();
BoundField TodayDate = new BoundField();
ImageField ImgField = new ImageField();

CusCode.HeaderText = "Customer Code";
CusName.HeaderText = "Customer Name";
CusAge.HeaderText = "Age";
CusEmail.HeaderText = "Email Address";
SomeDecimals.HeaderText = "Decimals";
TodayDate.HeaderText = "Date";
ImgField.HeaderText = "Favorites";

CusCode.DataField = "Cus_Code";
CusName.DataField = "Cus_Name";
CusAge.DataField = "Cus_Age";
CusEmail.DataField = "Cus_Email";
SomeDecimals.DataField = "SomeDecimals";
TodayDate.DataField = "Today";
ImgField.DataImageUrlField = "ImgUrl";
ImgField.DataImageUrlFormatString = “YourSiteCompleteUrl” + "{0}";
ImgField.ItemStyle.Height = Unit.Pixel(100);
ImgField.ItemStyle.Width = Unit.Pixel(140);

GridView g = new GridView();
g.AutoGenerateColumns = false;

g.RowDataBound += new GridViewRowEventHandler(g_RowDataBound);

g.Columns.Insert(0, CusCode);
g.Columns.Insert(1, CusName);
g.Columns.Insert(2, CusAge);
g.Columns.Insert(3, CusEmail);
g.Columns.Insert(4, SomeDecimals);
g.Columns.Insert(5, TodayDate);
g.Columns.Insert(6, ImgField);

g.DataSource = dtData;
Post Your Reply If you still have problem, post it again under this reply for better response.

Sponsored by
When Good HTML Goes Bad: How You Can Protect Your Web Applications
Everything You Know About CSS Is Wrong! - Free 37 Page Preview!
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!