Hi BugHunter,
Refer below sample.
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound"
DataKeyNames="CustomerId">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
<ItemTemplate>
<asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="EnableDisableCheckBox">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT(Country) FROM Customers");
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = this.ExecuteQuery(cmd);
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("Please select"));
}
}
private void BindGrid()
{
SqlCommand cmd = new SqlCommand("SELECT TOP 5 CustomerId, ContactName, Country FROM Customers");
gvCustomers.DataSource = this.ExecuteQuery(cmd);
gvCustomers.DataBind();
}
private DataTable ExecuteQuery(SqlCommand cmd)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void EnableDisableCheckBox(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
DropDownList ddl = row.FindControl("ddlCountries") as DropDownList;
CheckBox chk = row.FindControl("CheckBox1") as CheckBox;
if (ddl.SelectedIndex > 0)
{
chk.Enabled = true;
}
else
{
chk.Enabled = false;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cmd As SqlCommand = New SqlCommand("SELECT DISTINCT(Country) FROM Customers")
Dim ddlCountries As DropDownList = (TryCast(e.Row.FindControl("ddlCountries"), DropDownList))
ddlCountries.DataSource = Me.ExecuteQuery(cmd)
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
ddlCountries.Items.Insert(0, New ListItem("Please select"))
End If
End Sub
Private Sub BindGrid()
Dim cmd As SqlCommand = New SqlCommand("SELECT TOP 5 CustomerId, ContactName, Country FROM Customers")
gvCustomers.DataSource = Me.ExecuteQuery(cmd)
gvCustomers.DataBind()
End Sub
Private Function ExecuteQuery(ByVal cmd As SqlCommand) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub EnableDisableCheckBox(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvCustomers.Rows
Dim ddl As DropDownList = TryCast(row.FindControl("ddlCountries"), DropDownList)
Dim chk As CheckBox = TryCast(row.FindControl("CheckBox1"), CheckBox)
If ddl.SelectedIndex > 0 Then
chk.Enabled = True
Else
chk.Enabled = False
End If
Next
End Sub
Screenshot