i'm trying to fill DropDownList in GridView
What I have tried:
i have this class to call StoredProcedure :
public class DropDownList_DataAccessLayer
{
public static void DDL(DropDownList DDl, string ProcedureName, SqlParameter SPParameter, string DDLText, string DDLValue, string TXT)
{
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection CON = new SqlConnection(CS))
{
SqlCommand CMD = new SqlCommand();
CMD = CON.CreateCommand();
CMD.CommandType = CommandType.StoredProcedure;
if (SPParameter != null)
{
CMD.Parameters.Add(SPParameter);
}
CMD.Connection = CON;
CMD.CommandText = ProcedureName;
CON.Open();
SqlDataReader RD = CMD.ExecuteReader();
DDl.DataSource = RD;
DDl.DataTextField = DDLText;
DDl.DataValueField = DDLValue;
DDl.DataBind();
ListItem li = new ListItem(" Please Select " + TXT, "-1");
DDl.Items.Insert(0, li);
RD.Close();
CON.Close();
}
}
}
in Pageloade i have this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridViewData();
}
}
private void BindGridViewData()
{
int CenterCode = Convert.ToInt32(Request.QueryString["Center_Code"]);
GridView1.DataSource = ProjectDataAccessLayer.GetAllProjects(CenterCode);
GridView1.DataBind();
}
in GridView1_RowDataBound i have this :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ActiveCenterDDL = (DropDownList)e.Row.FindControl("ddlEditCenters");
DropDownList NewActiveCenterDDL = (DropDownList)e.Row.FindControl("ddlNewCenters");
DropDownList_DataAccessLayer.DDL(ActiveCenterDDL, "Get_ActiveCenters", null, "CenterName", "CenterCode", "Center"); // for
DropDownList_DataAccessLayer.DDL(NewActiveCenterDDL, "Get_ActiveCenters", null, "CenterName", "CenterCode", "Center");
in aspx i have this :
<asp:TemplateField HeaderText="Centers" SortExpression="Center_Name">
<itemtemplate>
<asp:Label ID="lblCenterName" runat="server" Text='<%# Bind("Center_Name") %>'>
<edititemtemplate>
<asp:DropDownList ID="ddlEditCenters" CssClass="gvedit" runat="server" ValidationGroup="EditRow">
<asp:RequiredFieldValidator CssClass="Validator" ID="ddlEditCentersValidator" runat="server" ControlToValidate="ddlEditCenters" ErrorMessage="you didn't select a center" InitialValue="-1" ValidationGroup="Edit">*
<footertemplate>
<asp:DropDownList ID="ddlNewCenters" CssClass="gvfooter" runat="server">