如何使用文本框控件在现有gridview中添加列 [英] how to add column in existing gridview with textbox control

查看:128
本文介绍了如何使用文本框控件在现有gridview中添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTML编码。它将显示三列col1,col2,col3。

现在我想在现有的gridview表中添加列col4按钮点击add_column()

请怎么做帮助我.......提前谢谢。



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>

推荐答案

点击按钮时添加列需要重新绑定数据。根据您所拥有的,已定义的列,如果要添加新列,则需要在单击添加按钮时在代码中为其编写。试用。

使用: gridView.Columns.Add()方法。
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.






您需要创建一个模板类来表示动态模板列。







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);





这会将模板化列添加到gridview。请注意,您需要再次执行数据绑定以查看新列。



希望这会有所帮助。



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.


这篇关于如何使用文本框控件在现有gridview中添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆