添加非数据绑定列以编程的GridView [英] add non databound column to gridview programmatically
问题描述
我要一列添加到GridView上的Page_Load()
I want to add a column to the gridview on page_load()
这是我想补充它的标签。我的数据源有ID的..但我不想显示身份证,我要回去看看的ID在我的对象模型和名称替换它。
Its a label that I want to add. My datasource has ID in it.. but I dont want to display Id, I have to look up the ID in my object model and replace it with name.
所以,我需要这样一个标签:
So, I need a label like this:
<asp:Label ID="1234" runat="server" OnDataBinding="BindName" />
这是什么我在ASCX文件做..里面的TemplateField。
This is something I am doing in the ascx file.. inside TemplateField.
我想这样做同样的事情在后面的编程code。
I want to do the same thing in the code behind programatically.
任何想法?
推荐答案
您需要创建一个实现了了Itemplate接口的类。
You need to create a class that implements that ITemplate interface.
public class TemplateImplementation : ITemplate
{
public void InstantiateIn(Control container)
{
Label label = new Label();
label.DataBinding += Label_DataBinding;
container.Controls.Add(label);
}
void Label_DataBinding(object sender, EventArgs e)
{
Label label = (Label)sender;
object dataItem = DataBinder.GetDataItem(label.NamingContainer);
string sName = /* Lookup your name using the dataitem here here */;
label.Text = sName;
}
}
您然后创建一个TemplateColumn中和你的ItemTemplate设置为此类的实例。
You then create a TemplateColumn and set your ItemTemplate to an instance of this class.
TemplateColumn lblColumn = new TemplateColumn();
lblColumn.ItemTemplate = as;
grdMyGrid.Columns.Add(lblColumn);
这篇关于添加非数据绑定列以编程的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!