Hi priyatrip,
I have created sample please refe the below code.
HTML
<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>
Code
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();
}
}
Screenshot
