Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to bind two DropDownList into a GridView in ASP.NET.

I did bind one DropDownList with Databound event, but the problem is how can I bind the second DropDownList in the GridView?

my requirement is 1 dropdownlist selected autometically second dropdownlist bind i tied the below code its not working plz help me any one....

C#
public DataSet GetHeadName()
    {
         _conn = new SqlConnection(str);
      _cmd = new SqlCommand("getheadname", _conn);
              _cmd.CommandType = CommandType.StoredProcedure;
              _cmd.CommandText = "getheadname";
              SqlDataAdapter da = new SqlDataAdapter(_cmd);
              DataSet ds = new DataSet();
              da.Fill(ds);
        return ds;
    }

    public DataSet GetArticalName()
    {
         _conn = new SqlConnection(str);
         _cmd = new SqlCommand("getarticlename", _conn);
              _cmd.CommandType = CommandType.StoredProcedure;
              _cmd.CommandText = "getarticlename";
              SqlParameter p1 = null;
              p1 = new SqlParameter();
              p1.ParameterName = "@articlename";
              p1.Value = "TMAST_HEAD_NAME";
              _cmd.Parameters.Add(p1);
              SqlDataAdapter da = new SqlDataAdapter(_cmd);
              DataSet ds = new DataSet();
              da.Fill(ds);
        return ds;
    }

 if (e.Row.RowType == DataControlRowType.DataRow)
          {             
              DropDownList ddlhead = (e.Row.FindControl("DropDownList1") as DropDownList);
              DropDownList ddlartical = (e.Row.FindControl("DropDownList2") as DropDownList);
              if (ddlhead != null && ddlartical != null)
              {

                  ddlhead.DataSource = GetHeadName();
                  ddlhead.DataTextField = "TMAST_HEAD_NAME";
                  ddlhead.DataValueField = "TMAST_HEAD_NAME";
                  ddlhead.DataBind();
                  //Add Default Item in the DropDownList
                  ddlhead.Items.Insert(0, new ListItem("--please select headname--"));
                 ddlhead.SelectedValue = Gridview1.DataKeys[e.Row.RowIndex].Values[0].ToString();

                  ddlhead.DataSource = GetArticalName();
                  //ddlhead.DataTextField = "TMAST_ARTICLE_NAME";
                  //ddlhead.DataValueField = "TMAST_ARTICLE_NAME";
                  ddlhead.DataBind();
                  //Add Default Item in the DropDownList
                  ddlhead.Items.Insert(0, new ListItem("--please select artical--"));
                 // ddlhead.SelectedValue = Gridview1.DataKeys[e.Row.RowIndex].Values[2].ToString();
              }

              }
          else
          {
              ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Source Not Available');", true);
          }
Posted
Updated 13-Dec-13 0:29am
v2

1 solution

 
Share this answer
 
v3
Comments
CHill60 13-Dec-13 7:20am    
Are you aware that you keep posting a link to a job search with your solutions? If you were aware then please stop it.

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