Hi akhter,
Pls refer below code.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField HeaderText="Name" DataField="ContactName" />
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' Visible="false" />
<asp:DropDownList ID="ddlCountries" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnRefresh" runat="server" Text="Refresh" OnClick="OnRefresh" />
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)
{
GridView1.DataSource = GetData("SELECT TOP 5 ContactName, Country FROM Customers");
GridView1.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = GetData("SELECT DISTINCT TOP 5 Country FROM Customers WHERE Country IS NOT NULL");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("Please select"));
string country = (e.Row.FindControl("lblCountry") as Label).Text;
if (ddlCountries.Items.FindByValue(country) != null)
{
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
}
protected void OnRefresh(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
DropDownList ddlCountries = (row.FindControl("ddlCountries") as DropDownList);
ddlCountries.Items.Clear();
// Selecting the updated record to bind the DropDownList.
ddlCountries.DataSource = GetData("SELECT DISTINCT TOP 10 Country FROM Customers WHERE Country IS NOT NULL");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
ddlCountries.Items.Insert(0, new ListItem("Please select"));
string country = (row.FindControl("lblCountry") as Label).Text;
if (ddlCountries.Items.FindByValue(country) != null)
{
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GridView1.DataSource = GetData("SELECT TOP 5 ContactName, Country FROM Customers")
GridView1.DataBind()
End If
End Sub
Private Function GetData(ByVal query As String) As DataSet
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand(query)
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As DataSet = New DataSet()
sda.Fill(ds)
Return ds
End Using
End Using
End Using
End Function
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddlCountries As DropDownList = (TryCast(e.Row.FindControl("ddlCountries"), DropDownList))
ddlCountries.DataSource = GetData("SELECT DISTINCT TOP 5 Country FROM Customers WHERE Country IS NOT NULL")
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
ddlCountries.Items.Insert(0, New ListItem("Please select"))
Dim country As String = TryCast(e.Row.FindControl("lblCountry"), Label).Text
If ddlCountries.Items.FindByValue(country) IsNot Nothing Then
ddlCountries.Items.FindByValue(country).Selected = True
End If
End If
End Sub
Protected Sub OnRefresh(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
Dim ddlCountries As DropDownList = (TryCast(row.FindControl("ddlCountries"), DropDownList))
ddlCountries.Items.Clear()
ddlCountries.DataSource = GetData("SELECT DISTINCT TOP 10 Country FROM Customers WHERE Country IS NOT NULL")
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
ddlCountries.Items.Insert(0, New ListItem("Please select"))
Dim country As String = TryCast(row.FindControl("lblCountry"), Label).Text
If ddlCountries.Items.FindByValue(country) IsNot Nothing Then
ddlCountries.Items.FindByValue(country).Selected = True
End If
Next
End Sub
Screenshot