如何将组合框添加到一个asp.net GridView控件绑定 [英] How to add a ComboBox to an asp.net unbound GridView
本文介绍了如何将组合框添加到一个asp.net GridView控件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道我可以一个ComboBox列未绑定的GridView的通过code在运行时添加的
I wonder how I can add a ComboBox column to an unbound GridView through code at runtime.
推荐答案
编程方式:
我用下面的类(但下拉列表和复选框绑定),其中实现了Itemplate了过去。
I've used the following class (but for DropDown and CheckBox binding) in the past which implements ITemplate.
public class AddTemplateToGridView : ITemplate
{
String columnName;
public AddTemplateToGridView(String colname)
{
columnName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
if (columnName == "yourField")
{
ComboBox cb = new ComboBox();
cb.DataBinding += new EventHandler(cb_DataBinding);
container.Controls.Add(cb);
}
}
void cb_DataBinding(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
GridViewRow container = (GridViewRow)cb.NamingContainer;
Object dataValue = DataBinder.Eval(container.DataItem, columnName);
if (dataValue != DBNull.Value)
{
// Assign ComboBox vals if necessary
... = dataValue
}
}
}
通过声明你的GridView和模板列在codebehind用途:
Use by declaring your GridView and TemplateField in the codebehind:
GridView newGrid = new GridView();
TemplateField field = new TemplateField();
field.HeaderText = "columnName";
field.ItemTemplate = // some item template
field.EditItemTemplate = new AddTemplateToGridView("yourField");
newGrid.Columns.Add(field);
或声明的:
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="yourField">
<ItemTemplate>
<asp:Label runat="server" Text ='<%# Eval("yourField") %>' />
</ItemTemplate>
<EditItemTemplate>
<%--Your ComboBox--%>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
希望这有助于。
这篇关于如何将组合框添加到一个asp.net GridView控件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文