Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 dropdownlist & 1 gridview in the form, how can we show the data in gridview that is depend on selectedindexedchanged event of single dropdownlist
Only single dropdownlist can work at a single time but we required both dropdownlist is working in the form
when we select 1st dropdownlist then show the data that will depend on 1st dropdownlist & when we select 2nd dropdownlist then show the data that will depend on dropdownlist
I have 2 dropdown in that gridview from same table
I am getting only 1st dropdownlist data & 2nd dropdownlist is not working
Posted
Comments
CHill60 14-Jul-14 7:11am    
Can't help you if we can't see your code
jo.him1988 15-Jul-14 2:20am    
you have to write onselectindex change of both dropdownlist
first find drop down parent(Row) than find your drop down control and than simple bind your drop down

1 solution

Default.aspx
// i have a two drop down inside gridview fst one for country seccond for state
//on load binding country ddl to country list and state dropdownlist to state list
//on fst ddl click i am binding it to state
//on state drop down click i am binding its country to fst ddl

XML
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="False"
         onrowdatabound="grd_RowDataBound"
         
 <Columns>

 <asp:TemplateField HeaderText="Country">
 <ItemTemplate >
 <asp:DropDownList ID="DropDownList1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"  Width="200px" AutoPostBack="true" EnableViewState="true" runat="server" >

 </asp:DropDownList>
 </ItemTemplate>
 </asp:TemplateField>
    <asp:TemplateField HeaderText="State">
 <ItemTemplate>
  <asp:DropDownList ID="DropDownList2" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"  Width="200px" AutoPostBack="true" EnableViewState="true" runat="server" >

 </asp:DropDownList>
 </ItemTemplate>
 </asp:TemplateField>
 </Columns>
 </asp:GridView>




Default.cs

// i am using edmx to bind DropDown in grid

protected void DropDownList1_SelectedIndexChanged(object sen, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                var ddl = (DropDownList)sen;

                GridViewRow row = (GridViewRow)ddl.Parent.Parent;
                DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
                int idx = row.RowIndex;
                if (ddl.SelectedIndex > 0)
                {
                    ddl2.Items.Clear();
                    var x = ddl.SelectedValue;
                    int id = Convert.ToInt32(x);
                    TestEntities en = new TestEntities();
                    ddl2.DataSource = en.State.Where(s => s.Country.CountryID == id).ToList();
                    ddl2.DataTextField = "StateName";
                    ddl2.DataValueField = "StateID";
                    ddl2.DataBind();
                    ddl2.Items.Insert(0, new ListItem("--Select State --", "0"));
                }
            }
        }
        protected void DropDownList2_SelectedIndexChanged(object sen, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                var ddl2 = (DropDownList)sen;

                GridViewRow row = (GridViewRow)ddl2.Parent.Parent;
                DropDownList ddl1 = (DropDownList)row.FindControl("DropDownList1");
                int idx = row.RowIndex;
                if (ddl2.SelectedIndex > 0)
                {
                    ddl1.Items.Clear();
                    var x = ddl2.SelectedValue;
                    int id = Convert.ToInt32(x);
                    TestEntities en = new TestEntities();
                  var contryId=  en.State.Where(s => s.StateID == id).First();
                  int cc = Convert.ToInt32(contryId.CountryReference.EntityKey.EntityKeyValues[0].Value);
                    ddl1.DataSource = en.Country.Where(s => s.CountryID ==cc ).ToList();
                    ddl1.DataTextField = "CountryName";
                    ddl1.DataValueField = "CountryID";
                    ddl1.DataBind();
                    ddl1.Items.Insert(0, new ListItem("--Select Country --", "0"));
                }
            }
        }





happy coding :) :) :)
 
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