Click here to Skip to main content
16,003,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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">
Posted
Updated 24-Oct-16 12:00pm
Comments
Suvendu Shekhar Giri 23-Oct-16 23:13pm    
What is the issue here?
Is it giving any error?

Something like below example:

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList ActiveCenterDDL= (DropDownList)e.Row.FindControl("ddlEditCenters");
            //bind dropdown-list
            DataTable dt = con.GetData("Select category_name from category");
            ActiveCenterDDL.DataSource = dt;
            ActiveCenterDDL.DataTextField = "category_name";
            ActiveCenterDDL.DataValueField = "category_name";
            ActiveCenterDDL.DataBind();

DropDownList NewActiveCenterDDL= (DropDownList)e.Row.FindControl("ddlEditCenters");
            //bind dropdown-list
            DataTable dt = con.GetData("Select category_name from category");
            NewActiveCenterDDL.DataSource = dt;
            NewActiveCenterDDL.DataTextField = "category_name";
            NewActiveCenterDDL.DataValueField = "category_name";
            NewActiveCenterDDL.DataBind();

            DataRowView dr = e.Row.DataItem as DataRowView;
            //ddList.SelectedItem.Text = dr["category_name"].ToString();
            ActiveCenterDDL.SelectedValue = dr["category_name"].ToString();
        }
    }
}
 
Share this answer
 
If I understand correctly, you wanted to populate your DropDownList from within EditItemTemplate. If that's the case then this article should help you with that: ASP.NET GridView: Implementing Cascading DropDownList on Edit Mode[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900