Hi amar,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlCountries" runat="server" OnSelectedIndexChanged="ddlCountries_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Text="United States" Value="United States"></asp:ListItem>
<asp:ListItem Text="India" Value="India"> </asp:ListItem>
<asp:ListItem Text="France" Value="France"></asp:ListItem>
<asp:ListItem Text="Russia" Value="Russia"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
int index = (ddl.NamingContainer as GridViewRow).RowIndex;
if (index < gvDetails.Rows.Count - 1)
{
GridViewRow nextRow = gvDetails.Rows[index + 1];
string id = nextRow.Cells[0].Text;
string name = nextRow.Cells[1].Text;
string country = (nextRow.FindControl("ddlCountries") as DropDownList).SelectedValue;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('ID: " + id + "\\nName: " + name + "\\nCountry: " + country + "');", true);
}
}
Screenshot