Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

XML
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false"
            >
    <Columns>
    <asp:TemplateField HeaderText="Col1">
    <ItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Col2">
    <ItemTemplate>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Col3">
    <ItemTemplate>
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>

    </Columns>
    </asp:GridView>
Posted
Updated 13-Jan-13 6:52am
v2
Comments
[no name] 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.

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.
 
Share this answer
 
Comments
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
Hi,

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



C#
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:
			//Header
			 container.Controls.Add(//probably a label);
			break;
		 case DataControlRowType.DataRow:
			//RowType
			 container.Controls.Add(//TextBox in your case);
			break;
                 }
    }
}


On button click, create a templated field.

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

GridView1.Columns.Add(fld);


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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900