如何绑定在GridView的一个DropDownList? [英] how to bind a dropdownlist in gridview?
问题描述
我在其中的每一行包含一个DropDownList的GridView。我想每一个DropDownList的动态绑定。谁能告诉我,我怎么能做到这一点。由于提前
I have a gridview in which every row contains a dropdownlist. I want to bind every dropdownlist dynamically. Can someone tell me how can i do it. Thanks in Advance
推荐答案
如果您正在使用模板列,那么你可以绑定从加价使用数据绑定前pressions您的下拉。例如,
If you are using template column then you can bind your drop-down from mark-up using data-binding expressions. For example,
<asp:TemplateField HeaderText="XYZ">
<ItemTemplate>
<asp:DropDownList runat="server" ID="MyDD" DataSourceId="MyDataSource" />
</ItemTemplate>
</asp:TemplateField>
以上是假设在整个行不断的下拉数据。如果它被改变,那么你可以使用数据绑定前pression如
Above is assuming that your drop-down data in constant across rows. If it is changing then you can use data-binding expression such as
<asp:DropDownList runat="server" DataSource='<%# GetDropDownData(Container) %>' DataTextField="Text" DataValueField="Value" />
GetDropDownData将在受保护的方法$ C $的c-后面,将返回的数据(数据表,列表,阵列)对于给定的行
GetDropDownData will be a protected method in code-behind that will return the data (data-table, list, array) for the given row.
您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx\">GridView.RowDataBound事件(或RowCreated事件)在code-背后,填补下拉菜单。例如,
You can use GridView.RowDataBound event (or RowCreated event) in code-behind to fill drop-downs. For example,
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Find the drop-down (say in 3rd column)
var dd = e.Row.Cells[2].Controls[0] as DropDownList;
if (null != dd) {
// bind it
}
/*
// In case of template fields, use FindControl
dd = e.Row.Cells[2].FindControl("MyDD") as DropDownList;
*/
}
}
这篇关于如何绑定在GridView的一个DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!