Click here to Skip to main content
15,905,073 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On my project I have to use three dropdownlists and the second one is populated from item selected from first one.
The problem exists there while I'm selecting a row from GridView. I can send those values to dropdownlist but the first DropDownList is not populating the second one if I select a row in gridview
The property of first DDL is set to IsPostBack because when I select an Item, it must populate the second DDL.
The code is as following:
DDL1
C#
public void ShowDDL1_Items()
       {

           SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
           try
           {

               using (sqlConn)
               {

                   SqlCommand sqlCmd = new SqlCommand("procDDL1", sqlConn);


                   sqlCmd.CommandType = CommandType.StoredProcedure;


                   sqlCmd.Parameters.Add(new SqlParameter("@ID_DDL1", SqlDbType.Int));


                   sqlCmd.Parameters["@ID_DDL1"].Value = Convert.ToInt32(Session["ID_Shkolla"]);


                   sqlConn.Open();


                   SqlDataReader reader = sqlCmd.ExecuteReader();


                   if (reader.HasRows)
                   {
                       DDL1.DataSource = reader;
                       DDL1.DataTextField = "FieldName";
                       DDL1.DataValueField = "ID_Field";
                       DDL1.DataBind();
                       DDL1.Items.Insert(0, new ListItem("--Select--", "0"));
                       DDL2.Items.Insert(0, new ListItem("--Select--", "0"));
                       DDL3.Items.Insert(0, new ListItem("--Select--", "0"));
                   }
               }
           }
           catch (Exception ex
           {
               throw ex;
           }
           finally
           {
               sqlConn.Close();
           }
       }


The DDL2
C#
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
       {

           SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
           try
           {

               using (sqlConn)
               {

                   SqlCommand sqlCmd = new SqlCommand("procDDL2", sqlConn);


                   sqlCmd.CommandType = CommandType.StoredProcedure;


                   sqlCmd.Parameters.Add(new SqlParameter("@ID_Field", SqlDbType.Int));


                   sqlCmd.Parameters["@ID_Field"].Value = Convert.ToInt32(ddListDrejtimi.SelectedValue);


                   sqlConn.Open();


                   SqlDataReader reader = sqlCmd.ExecuteReader();


                   if (reader.HasRows)
                   {
                       DDL2.DataSource = reader;
                       DDL2.DataTextField = "DDL2FieldName";
                       DDL2.DataValueField = "ID_Field2";
                       DDL2.DataBind();
                       DDL2.Items.Insert(0, new ListItem("--Select--", "0"));
                   }
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               sqlConn.Close();
           }
       }


How to populate DD2 if I select a row in gridview
C#
 protected void GV1_SelectedIndexChanged(object sender, EventArgs e)
        {

            DDL1.SelectedValue = GV1.SelectedRow.Cells[5].Text;
            DDL1.SelectedItem.Value = HttpUtility.HtmlDecode(GV1.SelectedRow.Cells[6].Text.ToString());}

///If I select a column for DDL2 it tells me that that value does not exist in DDL2 which means the DDl2 is not populated


Thank you in advance for your reply.
Posted

Try calling the event handler of DDL1 SelectedIndexChanged.
Something like-
C#
protected void GV1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //your existing code
            DDL1.SelectedValue = GV1.SelectedRow.Cells[5].Text;
            DDL1.SelectedItem.Value = HttpUtility.HtmlDecode(GV1.SelectedRow.Cells[6].Text.ToString());
            //add this line
            DDL1_SelectedIndexChanged(DDL1, EventArgs.Empty);
}


Hope, it helps :)

Note: Updated as suggested by @Richard in the comments.
 
Share this answer
 
v2
Comments
Richard Deeming 8-Dec-15 10:28am    
Nit-picking - you're calling the event handler, not the event. :)

And it would be better to pass the expected values to the handler:
DDL1_SelectedIndexChanged(DDL1, EventArgs.Empty);
Suvendu Shekhar Giri 8-Dec-15 11:04am    
True. Completely agree. Thanks :)
dr_iton 8-Dec-15 11:39am    
As you can see, I'm sending a parameter with stored procedure ("@ID_Field") which has an Int value so none of above suggestions works.
The parameter that I send is:
sqlCmd.Parameters.Add(new SqlParameter("@ID_Field", SqlDbType.Int));


sqlCmd.Parameters["@ID_Field"].Value = Convert.ToInt32(DDL1.SelectedValue);

Thank you
Solved the problem. In case that someone else has the same problem I'm writing the code below to solve the problem
C#
protected void GV1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this row gets the column with ID
            DDL1.SelectedValue = GV1.SelectedRow.Cells[5].Text;
            ///this code calls the event
            DDL1_SelectedIndexChanged(this, EventArgs.Empty);

            }


Thanks to all who helped me.
Cheers.
 
Share this answer
 
v2

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