acadia
acadia

Reputation: 2333

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 in the Gridview in ASP.net.

Thanks

Upvotes: 2

Views: 6058

Answers (2)

Jitendra Sawant
Jitendra Sawant

Reputation: 708

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

Upvotes: 1

Dan
Dan

Reputation: 17445

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;

}

Upvotes: 3

Related Questions