Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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"
            >
    <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 11-Jan-13 1:08am
Edited 13-Jan-13 7:52am
v2
Comments
surendra00 at 13-Jan-13 12:53pm
   
sir, i have updated my question....Please help me.
Rate this: bad
good
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.
  Permalink  
Comments
surendra00 at 12-Jan-13 3:11am
   
can u write code for this .....Please
Sandeep Mewara at 12-Jan-13 3:37am
   
What would you do if I do this?
 
surendra00 at 13-Jan-13 10:15am
   
sir ,please solve my problem thanks in advance
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
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:
			//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.
 
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.
  Permalink  

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

  Print Answers RSS
0 Mika Wendelius 191
1 Peter Leow 150
2 TheRealSteveJudge 129
3 Abhinav S 125
4 Suvendu Shekhar Giri 100
0 Sergey Alexandrovich Kryukov 8,513
1 OriginalGriff 6,526
2 Peter Leow 3,657
3 Zoltán Zörgő 3,426
4 Richard MacCutchan 2,417


Advertise | Privacy | Mobile
Web01 | 2.8.150123.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2015
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