Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
HTML Markup
The following
HTML Markup consists of:
GridView - For displaying data.
Columns
There are one BoundField and TemplateField columns.
TemplateField – The TemplateField column consists of ItemTemplate
<asp:GridView ID="gvCustomers" 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>
Binding the GridView
Inside the
Page_Load event handler, the
GetData is assigned to the
DataSource property of
GridView and the
GridView is populated.
Inside the
GetData method, first the connection string is read from the
Web.Config file.
Finally, the records are fetched from the
Customers Table of
SQL Server database using
SqlDataAdapter.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvCustomers.DataSource = GetData("SELECT ContactName, Country FROM Customers");
gvCustomers.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;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
gvCustomers.DataSource = GetData("SELECT ContactName, Country FROM Customers")
gvCustomers.DataBind()
End If
End Sub
Private Function GetData(query As String) As DataSet
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
sda.Fill(ds)
Return ds
End Using
End Using
End Using
End Function
Binding the ASP.Net DropDownList in ItemTemplate
C#
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
//Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, new ListItem("Please select"));
// Select the Country of Customer in DropDownList
string country = (e.Row.FindControl("lblCountry")as Label).Text;
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
VB.Net
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
'Find the DropDownList in the Row
Dim ddlCountries As DropDownList = CType(e.Row.FindControl("ddlCountries"), DropDownList)
ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers")
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
'Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, New ListItem("Please select"))
' Select the Country of Customer in DropDownList
Dim country As String = CType(e.Row.FindControl("lblCountry"),Label).Text
ddlCountries.Items.FindByValue(country).Selected = True
End If
End Sub
Screenshot
Demo
Downloads