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
Grid.Columns.Add(hyfield);

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

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

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

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

panel1.Controls.Add(Grid);

Read 4439 times
Super User

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

Latest discussions

  • No posts to display.