Hi Vincenzo67,
Please refer below sample.
HTML
<form id="form1" runat="server">
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CIGM" HeaderText="CIGM" ReadOnly="true" />
<asp:TemplateField
HeaderText="tCon">
<ItemTemplate>
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="OnSelectedIndexChanged" AutoPostBack="true" BackColor="Yellow" CssClass="ddl_Class_new">
<asp:ListItem Text="[ === === === ]" Value=""></asp:ListItem>
<asp:ListItem Text="OK" Value="OK"></asp:ListItem>
<asp:ListItem Text="KO" Value="KO"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl2" runat="server" OnSelectedIndexChanged="OnSelectedIndexChanged" AutoPostBack="true" BackColor="Yellow" CssClass="ddl_Class_new">
<asp:ListItem Text="[ === === === ]" Value=""></asp:ListItem>
<asp:ListItem Text="OK" Value="OK"></asp:ListItem>
<asp:ListItem Text="KO" Value="KO"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblDdl1" runat="server"></asp:Label><br />
<asp:Label ID="lblDdl2" runat="server"></asp:Label>
</form>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
gvProducts.DataSource = RetrieveProducts();
gvProducts.DataBind();
}
private DataTable RetrieveProducts()
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
using (SqlConnection myConnectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM tb_t", myConnectionString))
{
cmd.CommandTimeout = 2147483;
cmd.Connection.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.Clear();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
if (ds.Tables.Count > 0)
{
dt = ds.Tables[0];
}
}
}
return dt;
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
DropDownList ddl1 = (DropDownList)row.FindControl("ddl1");
DropDownList ddl2 = (DropDownList)row.FindControl("ddl2");
lblDdl1.Text = ddl1.SelectedValue;
lblDdl2.Text = ddl2.SelectedValue;
}
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
Private Sub BindGrid()
gvProducts.DataSource = RetrieveProducts()
gvProducts.DataBind()
End Sub
Private Function RetrieveProducts() As DataTable
Dim dt As DataTable = New DataTable()
Dim ds As DataSet = New DataSet()
Using myConnectionString As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM tb_t", myConnectionString)
cmd.CommandTimeout = 2147483
cmd.Connection.Open()
cmd.CommandType = CommandType.Text
cmd.Parameters.Clear()
Dim adapter As SqlDataAdapter = New SqlDataAdapter(cmd)
adapter.Fill(ds)
If ds.Tables.Count > 0 Then
dt = ds.Tables(0)
End If
End Using
End Using
Return dt
End Function
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim row As GridViewRow = CType(ddl.NamingContainer, GridViewRow)
Dim ddl1 As DropDownList = CType(row.FindControl("ddl1"), DropDownList)
Dim ddl2 As DropDownList = CType(row.FindControl("ddl2"), DropDownList)
lblDdl1.Text = ddl1.SelectedValue
lblDdl2.Text = ddl2.SelectedValue
End Sub
Screenshot