I have requirement to bind ASP.Net DropDownList control in EditItemTemplate of GridView. I have a edit imagebutton with commandname=\"Edit\"also dropdown needs to be binded
Your forgot to bind the dropdown list
DStatusEdit.DataBind();
protected void gv_RowDataBound(object sender, GridViewEditEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList DStatusEdit= (DropDownList)e.Row.FindControl("DStatusEdit");
DataTable dt = con.GetData("select distinct status from directory");
DStatusEdit.DataSource = dt;
DStatusEdit.DataTextField = "status";
DStatusEdit.DataValueField = "status";
DStatusEdit.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
DStatusEdit.SelectedValue = dr["columnname"].ToString();
}
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
gridviewBind();// your gridview binding function
}
I think this is helpful
private void bind_gridview_with_dropdownlist()
{
DataSet ds = edtcoursedal.Loaddropdown();
foreach (GridViewRow grdRow in GridView1.Rows)
{
DropDownList bind_dropdownlist = (DropDownList)(GridView1.FooterRow.Cells[3].FindControl("drplmaincourse"));
bind_dropdownlist.DataSource = ds;
bind_dropdownlist.DataTextField = "C_name";
bind_dropdownlist.DataValueField = "MId";
bind_dropdownlist.DataBind();
}
bind_dropdownlist.Items.Insert(0,new ListItem("Select","0"));
}
Code: Try this
protected void gv_RowDataBound(object sender, GridViewEditEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList ddList= (DropDownList)e.Row.FindControl("DStatusEdit");
//bind dropdownlist
DataTable dt = con.GetData("select distinct status from directory");
ddList.DataSource = dt;
ddList.DataTextField = "YourCOLName";
ddList.DataValueField = "YourCOLName";
ddList.DataBind();
DataRowView dr = e.Row.DataItem as DataRowView;
//ddList.SelectedItem.Text = dr["YourCOLName"].ToString();
ddList.SelectedValue = dr["YourCOLName"].ToString();
}
}
}
Have already answer similar question Binding dropdownlist inside gridview edititemtemplate
View Blog Article : How to bind drop-down list inside gridview edit template
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) {
using (SqlConnection con = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT GatePassNo,PurposeOfVisit FROM VisitorList"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
DropDownList ddlpurposeofvisit = (DropDownList)e.Row.FindControl("ddlpurposeofvisit");
ddlpurposeofvisit.DataSource = cmd.ExecuteReader();
ddlpurposeofvisit.DataTextField = "PurposeOfVisit";
ddlpurposeofvisit.DataValueField = "GatePassNo";
ddlpurposeofvisit.DataBind();
con.Close();
}
}
}