Hi ahmadsubuhanlubis,
Refer below sample.
HTML
<asp:GridView runat="server" ID="gvCustomers">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:DropDownList ID="ddlName" runat="server">
<asp:ListItem Text="--Select Name--" Value="0" />
<asp:ListItem Text="John Hammond" Value="1" />
<asp:ListItem Text="Mudassar Khan" Value="2" />
<asp:ListItem Text="Suzanne Mathews" Value="3" />
<asp:ListItem Text="Robert Schidner" Value="4" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:DropDownList ID="ddlCountry" runat="server">
<asp:ListItem Text="--Select Country--" Value="0" />
<asp:ListItem Text="USA" Value="1" />
<asp:ListItem Text="India" Value="2" />
<asp:ListItem Text="Russia" Value="3" />
<asp:ListItem Text="UK" Value="4" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=ddlName]").select2();
$("[id*=ddlCountry]").select2();
});
</script>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name2") });
dt.Rows.Add("abc");
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
protected void Submit(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCustomers.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)row.FindControl("ddlName");
DropDownList ddl2 = (DropDownList)row.FindControl("ddlCountry");
string message = "Name: " + ddl.SelectedItem.Text;
message += "\\nCountry: " + ddl2.SelectedItem.Text;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "')", true);
}
}
}
VB.Net
Protected Sub Page_load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Name2")})
dt.Rows.Add("abc")
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End If
End Sub
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvCustomers.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim ddl As DropDownList = CType(row.FindControl("ddlName"), DropDownList)
Dim ddl2 As DropDownList = CType(row.FindControl("ddlCountry"), DropDownList)
Dim message As String = "Name: " & ddl.SelectedItem.Text
message += "\nCountry: " & ddl2.SelectedItem.Text
ClientScript.RegisterStartupScript(Me.[GetType](), "alert", "alert('" & message & "')", True)
End If
Next
End Sub
Screenshot