I have 4 Dropdowns Dropdown contains values from 1 to 4 If in dropdown D1, values 2 is selected then for remaining downdowns values 1,3,4 will be available
If in dropdown D2, values 4 is selected then for remaining downdowns values 1,3 will be available

IF i select values as 1 in first dropdown and then select 2 in the second dropdown then 3,4 values remains for the remains dropdowns but I'm stuck as if I change the value of dropdown first from 1 to 3 then logically 1,4 should be available for selection by other dropdowns but it isn't happening.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChangingDDvalues
{
    public partial class DDchange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("State"), new DataColumn("Country"), new DataColumn("City") });
                dt.Rows.Add(1, "UP", "India", "Agra<br/>Delhi<br/>Shimla");
                dt.Rows.Add(2, "US", "America", "New york<br/>New Gercy<br/>Milan");
                dt.Rows.Add(3, "Chicago", "Illinois", "Manipolis<br/>Perth<br/>Balino");
                gvDetails.DataSource = dt;
                gvDetails.DataBind();
            }
        }

        protected void SelectionChanged(object sender, EventArgs e)
        {
            DropDownList current = sender as DropDownList;
            GridViewRow row = current.NamingContainer as GridViewRow;
            List<DropDownList> others = new List<DropDownList>();
            foreach (Control item in row.Controls[4].Controls)
            {
                if (item.GetType() == typeof(DropDownList))
                {
                    if (((DropDownList)item).ID != current.ID)
                    {
                        others.Add(item as DropDownList);
                    }
                }
            }
            DisableSelectedText(current, others.ToArray());
        }

        protected void DisableSelectedText(DropDownList ddlCurrent, DropDownList[] others)
        {

            foreach (DropDownList item in others)
            {
                item.Items.Remove(ddlCurrent.SelectedItem.Value);
            }
        }

        private DataTable GetFlag()
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Text", typeof(string)), new DataColumn("Value", typeof(int)) });
            dt.Rows.Add(1, 1);
            dt.Rows.Add(2, 2);
            dt.Rows.Add(3, 3);
            dt.Rows.Add(4, 4);
            return dt;
        }

        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataTable dt = GetFlag();
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddl1 = e.Row.FindControl("ddl1") as DropDownList;
                DropDownList ddl2 = e.Row.FindControl("ddl2") as DropDownList;
                DropDownList ddl3 = e.Row.FindControl("ddl3") as DropDownList;
                ddl1.DataSource = dt;
                ddl1.DataTextField = "Text";
                ddl1.DataValueField = "Value";
                ddl1.DataBind();
                ddl2.DataSource = dt;
                ddl2.DataTextField = "Text";
                ddl2.DataValueField = "Value";
                ddl2.DataBind();
                ddl3.DataSource = dt;
                ddl3.DataTextField = "Text";
                ddl3.DataValueField = "Value";
                ddl3.DataBind();
            }
        }
    }
}


<form id="form1" runat="server">
    <asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" Visible="false" />
        <asp:BoundField DataField="State" HeaderText="State" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
        <asp:BoundField DataField="City" HeaderText="City" HtmlEncode="false" />
        <asp:TemplateField HeaderText="State Flag">
            <ItemTemplate>
                <asp:DropDownList runat="server" Width="75px" ID="ddl1" OnSelectedIndexChanged="SelectionChanged"
                    AutoPostBack="true">
                </asp:DropDownList>
                <br />
                <asp:DropDownList runat="server" Width="75px" ID="ddl2" OnSelectedIndexChanged="SelectionChanged"
                    AutoPostBack="true">
                </asp:DropDownList>
                <br />
                <asp:DropDownList runat="server" Width="75px" ID="ddl3" OnSelectedIndexChanged="SelectionChanged"
                    AutoPostBack="true">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
        </form>

1 Answers

0
Ravi Sharma On Best Solutions

enter image description here

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div id="div1">
                <asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" AutoPostBack="true">
                     <asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                    <asp:ListItem Value="3" Text="3"></asp:ListItem>
                    <asp:ListItem Value="4" Text="4"></asp:ListItem>
                </asp:DropDownList>
            </div><br /><br />
             <div id="div2">
                <asp:DropDownList ID="ddl2" runat="server" OnSelectedIndexChanged="ddl2_SelectedIndexChanged" AutoPostBack="true">
                      <asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                    <asp:ListItem Value="3" Text="3"></asp:ListItem>
                    <asp:ListItem Value="4" Text="4"></asp:ListItem>
                </asp:DropDownList>
            </div><br /><br />
             <div id="div3">
                <asp:DropDownList ID="ddl3" runat="server" OnSelectedIndexChanged="ddl3_SelectedIndexChanged" AutoPostBack="true">
                      <asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                    <asp:ListItem Value="3" Text="3"></asp:ListItem>
                    <asp:ListItem Value="4" Text="4"></asp:ListItem>
                </asp:DropDownList>
            </div><br /><br />
             <div id="div4">
                <asp:DropDownList ID="ddl4" runat="server" OnSelectedIndexChanged="ddl4_SelectedIndexChanged" AutoPostBack="true">
                      <asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
                    <asp:ListItem Value="1" Text="1"></asp:ListItem>
                    <asp:ListItem Value="2" Text="2"></asp:ListItem>
                    <asp:ListItem Value="3" Text="3"></asp:ListItem>
                    <asp:ListItem Value="4" Text="4"></asp:ListItem>
                </asp:DropDownList>
            </div>
        </div>
    </form>
</body>
</html>



------------------------------aspx.cs code-----------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddl1.SelectedValue == ddl1.SelectedValue)
        {
            ddl2.Items.Remove(ddl1.SelectedValue);
            ddl3.Items.Remove(ddl1.SelectedValue);
            ddl4.Items.Remove(ddl1.SelectedValue);
        }
    }

    protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl2.SelectedValue == ddl2.SelectedValue)
        {
            ddl1.Items.Remove(ddl2.SelectedValue);
            ddl3.Items.Remove(ddl2.SelectedValue);
            ddl4.Items.Remove(ddl2.SelectedValue);
        }
    }

    protected void ddl3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl3.SelectedValue == ddl3.SelectedValue)
        {
            ddl1.Items.Remove(ddl3.SelectedValue);
            ddl2.Items.Remove(ddl3.SelectedValue);
            ddl4.Items.Remove(ddl3.SelectedValue);
        }
    }

    protected void ddl4_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl4.SelectedValue == ddl4.SelectedValue)
        {
            ddl1.Items.Remove(ddl4.SelectedValue);
            ddl2.Items.Remove(ddl4.SelectedValue);
            ddl3.Items.Remove(ddl4.SelectedValue);
        }
    }
}