开发者

Setting selectedvalue for a dropdownlist in GridView

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 i开发者_如何学Cn the Gridview in ASP.net.

Thanks


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;

}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜