我的Gridview中有一个下拉列表,我将数据源绑定到gridview。
虽然所有记录都正确显示,但未选择下拉值。
如何设置类似
的内容 <%# Bind("Country") %>
获取ASP.net中Gridview的下拉列表。
由于
答案 0 :(得分:2)
您可以为网格视图挂钩RowDataBound事件,找到控件并设置值。
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
dropdownList .SelectedIndex = SET_VALUE_HERE;
}
答案 1 :(得分:0)
从数据源设置DropDownList值应该是:
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
ddlCountry.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
}
}