Click here to Skip to main content
13,350,708 members (57,516 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have this html coding. it will display three columns col1,col2,col3.
Now i want to add column col4 in the existing gridview table on button click add_column()
how to do this please help me.......thanks in advance.

<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false"

    <asp:TemplateField HeaderText="Col1">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TemplateField HeaderText="Col2">
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:TemplateField HeaderText="Col3">
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

Posted 11-Jan-13 1:08am
Updated 13-Jan-13 7:52am
pranathis012 11-Jan-13 6:09am
your question is not clear
surendra00 13-Jan-13 12:53pm
sir, i have updated my question....Please help me.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Adding column on button click would need a rebind of data. Based on what you have, defined columns, if you want to add a new column you need to write for it in your code behind when you click add button. Try out.
Use: gridView.Columns.Add() method.
surendra00 12-Jan-13 3:11am
can u write code for this .....Please
Sandeep Mewara 12-Jan-13 3:37am
What would you do if I do this?

surendra00 13-Jan-13 10:15am
sir ,please solve my problem thanks in advance
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


You need to create a template class to represent a dynamic template column.

public class MyTemplate : ITemplate
 private DataControlRowType tempType;
 private string colName;

 public MyTemplate(DataControlRowType type, string col)
 tempType = type;
 colName = col;

//Method of ITemplate Interface, called on databinding of gridview.

public void InstantiateIn(Control container)

// Define the col Header and Row type in here.
switch (templateType)
                case DataControlRowType.Header:
			 container.Controls.Add(//probably a label);
		 case DataControlRowType.DataRow:
			 container.Controls.Add(//TextBox in your case);

On button click, create a templated field.

TemplateField fld = new TemplatedField();
fld.ItemTemplate = new MyTemplate(DataControlRowType.DataRow, "col4");
fld.HeaderTemplate = new MyTemplate(DataControlRowType.Header, "col4");


This will add the templated column to your gridview. Note that, you need to do a databind again to view your new column.

Hope this helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100