Setting selectedvalue for a dropdownlist in GridView

梦想的初衷 提交于 2019-12-12 18:18:22

问题


I have a dropdownlist in my Gridview and I am binding a datasource to the gridview.

Though all the records are displaying properly the dropdown value is not selected.

How do I set something like

<%# Bind("Country") %> for a dropdownlist in the Gridview in ASP.net.

Thanks


回答1:


You can hook into the RowDataBound event for the grid view, find the control and set the value.

protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{

     var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
     dropdownList .SelectedIndex = SET_VALUE_HERE;

}



回答2:


Setting DropDownList value from a Datasource should be like:

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


来源:https://stackoverflow.com/questions/1867499/setting-selectedvalue-for-a-dropdownlist-in-gridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!