Hi comunidadmexi..,
Please refer below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" DataKeyNames="CustomerId">
<Columns>
<asp:TemplateField HeaderText="Id" ItemStyle-Width="30">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%# Eval("CustomerId") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:BoundField HeaderText="Country" DataField="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" Text="DetailsView" ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:DropDownList ID="ddlYesNo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Text="" Value="0" />
<asp:ListItem Text="Y" Value="1" />
<asp:ListItem Text="N" Value="3" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
Namespace
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlYesNo = (DropDownList)sender;
GridViewRow gvRow = (sender as DropDownList).NamingContainer as GridViewRow;
string sIDdataKey = this.gvCustomers.DataKeys[gvRow.RowIndex].Values[0].ToString();
string name = gvRow.Cells[1].Text;
lblMessage.Text = sIDdataKey + "<br />" + name;
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT CustomerId, Name, Country FROM Customers";
cmd.Connection = con;
con.Open();
this.gvCustomers.DataSource = cmd.ExecuteReader();
this.gvCustomers.DataBind();
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddlYesNo As DropDownList = CType(sender, DropDownList)
Dim gvRow As GridViewRow = TryCast((TryCast(sender, DropDownList)).NamingContainer, GridViewRow)
Dim sIDdataKey As String = Me.gvCustomers.DataKeys(gvRow.RowIndex).Values(0).ToString()
Dim name As String = gvRow.Cells(1).Text
lblMessage.Text = sIDdataKey & "<br />" & name
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT CustomerId, Name, Country FROM Customers"
cmd.Connection = con
con.Open()
Me.gvCustomers.DataSource = cmd.ExecuteReader()
Me.gvCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Screenshot