Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

Bind DropDownList inside a GridView

$
0
0

In My GridView i have 3 dropdownlist. I have binded the 1st dropdown with SQLdata-table. What i need is to bind the second Dropdown(which is in side the same gridview) to database based on the value of the 1st drodown and similarly the 3rd one. I have made the on TextChange event for the first dropdown to call the method of binding the 2nd dropdownlist. The 2nd dropdown is not showing any data. Plz Help!!! here is my code..

protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            scon.Open();
            var ddl2 = (DropDownList)e.Row.FindControl("DropDownList2");
            SqlCommand cmd = new SqlCommand("select parent,parentid from bdwnmast", scon);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            scon.Close();
            ddl2.DataSource = ds;
            ddl2.DataTextField = "parent";
            ddl2.DataValueField = "parentid";
            ddl2.DataBind();
            ddl2.Items.Insert(0, new ListItem("---Select---", "0"));
            scon.Close();
            return;
        }
    }
protected void itemChanged(object sender, EventArgs e)//ontextchange event for the first dropdown
    {
        itemChanged_ddl3(sender);
    }
protected void itemChanged_ddl3(object sender, EventArgs e)
    {
        var ri = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
        DropDownList ddl2 = (DropDownList)GridView1.Rows[ri].FindControl("dropdownlist2");
        DropDownList ddl3 = (DropDownList)GridView1.Rows[ri].FindControl("dropdownlist3");
        if (!isPostBack)
        {

            scon.Open();
            SqlCommand cmd3 = new SqlCommand("select child,childid from bdwntree where parentid='"+ddl2.SelectedItem.Value+"'", scon);
            SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
            DataSet ds3 = new DataSet();
            da3.Fill(ds3);
            scon.Close();
            ddl3.DataSource = ds3;
            ddl3.DataTextField = "child";
            ddl3.DataValueField = "childid";
            ddl3.DataBind();
            ddl3.Items.Insert(0, new ListItem("---Select---", "0"));
            scon.Close();
            itemchange_ddl4(sender);
            var ddl4 = (DropDownList)GridView1.Rows[ri].FindControl("dropdownlist4");
            ddl4.Enabled = true;
            return;
        }


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>