Print this page
Sunday, 30 December 2012 15:58

how to add dynamic column field in GridView control

Written by 
Rate this item
(1 Vote)

you can easily add dynamic column in GridView control
with data.there are many dynamic fields you can add
like hyperlink field,bound field,etc.

first create the fields then add those fields as
columns -

GridView Grid = new GridView();

HyperLinkField hyfield = new HyperLinkField();
hfield.HeaderText = "Forums";
hfield.HeaderStyle.BackColor = System.Drawing.Color.LightBlue;
hfield.DataNavigateUrlFields = new string[] { "Fid" };
hfield.DataNavigateUrlFormatString = "ViewForum.aspx?Forum={0}";
hfield.DataTextField = "ForumName";
hfield.ItemStyle.Width = 400; //column width

BoundField bfield1 = new BoundField();
bfield1.HeaderText = "Topics";
bfield1.HeaderStyle.BackColor = System.Drawing.Color.LightBlue;
bfield1.DataField = "TopicsCount";

BoundField bfield2 = new BoundField();
bfield2.HeaderText = "Posts";
bfield2.HeaderStyle.BackColor = System.Drawing.Color.LightBlue;
bfield2.DataField = "PostsCount";

Grid.AutoGenerateColumns = false;
Grid.BackColor = System.Drawing.Color.LightBlue;
Grid.DataSource = Data;

sometimes it's needed to add the dynamic GridView control
to any placeholder control like panel to display.


Read 3922 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments