Hi indradeo,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assign">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnUpdate" Text="Update" runat="server" OnClick="Update"
CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList);
DropDownList DropDownList2 = (e.Row.FindControl("DropDownList2") as DropDownList);
DataTable dt = new DataTable();
dt.Columns.Add("STATUS_NAME");
dt.Rows.Add("Status 1");
dt.Rows.Add("Status 2");
dt.Rows.Add("Status 3");
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "STATUS_NAME";
DropDownList1.DataValueField = "STATUS_NAME";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--SELECT STATUS--", "0"));
dt.Rows.Clear();
dt.Rows.Add("Assign 1");
dt.Rows.Add("Assign 2");
dt.Rows.Add("Assign 3");
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "STATUS_NAME";
DropDownList2.DataValueField = "STATUS_NAME";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("--SELECT NAME--", "0"));
}
}
protected void Update(object sender, EventArgs e)
{
GridViewRow row = (sender as Button).NamingContainer as GridViewRow;
DropDownList dropDownList1 = row.FindControl("DropDownList1") as DropDownList;
DropDownList dropDownList2 = row.FindControl("DropDownList2") as DropDownList;
if (dropDownList1.SelectedIndex > 0)
{
dropDownList1.Enabled = false;
}
if (dropDownList2.SelectedIndex > 0)
{
dropDownList2.Enabled = false;
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Id")
dt.Rows.Add(1)
dt.Rows.Add(2)
dt.Rows.Add(3)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim DropDownList1 As DropDownList = (TryCast(e.Row.FindControl("DropDownList1"), DropDownList))
Dim DropDownList2 As DropDownList = (TryCast(e.Row.FindControl("DropDownList2"), DropDownList))
Dim dt As DataTable = New DataTable()
dt.Columns.Add("STATUS_NAME")
dt.Rows.Add("Status 1")
dt.Rows.Add("Status 2")
dt.Rows.Add("Status 3")
DropDownList1.DataSource = dt
DropDownList1.DataTextField = "STATUS_NAME"
DropDownList1.DataValueField = "STATUS_NAME"
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, New ListItem("--SELECT STATUS--", "0"))
dt.Rows.Clear()
dt.Rows.Add("Assign 1")
dt.Rows.Add("Assign 2")
dt.Rows.Add("Assign 3")
DropDownList2.DataSource = dt
DropDownList2.DataTextField = "STATUS_NAME"
DropDownList2.DataValueField = "STATUS_NAME"
DropDownList2.DataBind()
DropDownList2.Items.Insert(0, New ListItem("--SELECT NAME--", "0"))
End If
End Sub
Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow)
Dim dropDownList1 As DropDownList = TryCast(row.FindControl("DropDownList1"), DropDownList)
Dim dropDownList2 As DropDownList = TryCast(row.FindControl("DropDownList2"), DropDownList)
If dropDownList1.SelectedIndex > 0 Then
dropDownList1.Enabled = False
End If
If dropDownList2.SelectedIndex > 0 Then
dropDownList2.Enabled = False
End If
End Sub
Screenshot