Hi landomarossi,
When you are using BoundField you will be able to find the value using the Cell index, but for TemplateField you need to use FindControl method and find the Label inside it.
Then using the Text property get the value.
Refer below sample code for both BoundField and TemplateField.
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" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Id" />
<asp:TemplateField HeaderText="CustomerId">
<ItemTemplate>
<asp:Label ID="lblCustomerId" runat="server" Text='<%#Eval("CustomerId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="OnSelectedIndexChanged"
AutoPostBack="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript">
function OpenAlert(message) {
alert(message);
}
</script>
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 (!this.IsPostBack)
{
this.BindGridView();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
DropDownList ddlcountry = (e.Row.FindControl("ddlCountry") as DropDownList);
con.Open();
ddlcountry.DataTextField = "Country";
ddlcountry.DataValueField = "CustomerId";
ddlcountry.DataSource = cmd.ExecuteReader();
ddlcountry.DataBind();
con.Close();
}
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = (sender as DropDownList).NamingContainer as GridViewRow;
string id = row.Cells[0].Text;
string customerId = (row.FindControl("lblCustomerId") as Label).Text;
string message = string.Format("Selected Id:\\n{0}\\n{1}", id, customerId);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
private void BindGridView()
{
string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGridView()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim constring As String = ConfigurationManager.ConnectionStrings("constring").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
Dim ddlcountry As DropDownList = TryCast(e.Row.FindControl("ddlCountry"), DropDownList)
con.Open()
ddlcountry.DataTextField = "Country"
ddlcountry.DataValueField = "CustomerId"
ddlcountry.DataSource = cmd.ExecuteReader()
ddlcountry.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = TryCast((TryCast(sender, DropDownList)).NamingContainer, GridViewRow)
Dim id As String = row.Cells(0).Text
Dim customerId As String = (TryCast(row.FindControl("lblCustomerId"), Label)).Text
Dim message As String = String.Format("Selected Id:\n{0}\n{1}", id, customerId)
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Private Sub BindGridView()
Dim constring As String = ConfigurationManager.ConnectionStrings("constring").ConnectionString
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End Sub