how to bind dropdownlist for edit item template in gridview using c#

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if ((e.Row.RowState & DataControlRowState.Edit) > 0)
                {
                    var ddl = (DropDownList)e.Row.Cells[1].FindControl("ddlCity");
                    ddl.DataSource = GetRecordsCities();
                    ddl.DataValueField = "ID";
                    ddl.DataTextField = "CityName";
                    ddl.DataBind();
                    var dr = e.Row.DataItem as DataRowView;
                    if (dr != null)
                    {
                        ddl.SelectedValue = dr[1].ToString();
                    }
                }
            }
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s