To nest the DropDownList control to GridView control is very helpful to show the data by selectable criteria. The DropDownList control can be easily nested to the GridView control. In this sample, each DropDownList is binded for different data. For instance, we can use GridView to show each category data in Northwind database, while we can use DropDownList to show all products under the selected category in each line. We will show you this tutorial by ASP.NET 2.0 and VB.NET.
To nest the DropDownList control to GridView control is very helpful to show the data by selectable criteria. The DropDownList control can be easily nested to the GridView control. In this sample, each DropDownList is binded for different content. For instance, we can use GridView to show each category data in northwind database, and we can use DropDownList to show all products under the selected category in each line.
First, to connect to the sample database, you will need to import the System.Data.SqlClient namespace.
| Imports System.Data.SqlClient |
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
Then please create a getdataset. And add a string query for data query in the sample database.
|
Private Function getdataset() As Data.DataSet
Dim connectionstring As String = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password="
Dim query As String = "select p.categoryid,p.productid, p.productname,c.categoryid,c.categoryname from products p,categories c where p.categoryid=c.categoryid and c.categoryid<3"
Dim myconnection As New SqlConnection(connectionstring)
Dim ad As New SqlDataAdapter(query, myconnection)
Dim ds As New Data.DataSet()
ad.Fill(ds)
Return ds
End Function
|
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
Binding DropDownList to GridView.
Protected Sub gridview1_rowdatabound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview1.RowDataBound
Dim mytable As New Data.DataTable()
Dim productidcolumn As New Data.DataColumn("productid")
Dim productnamecolumn As New Data.DataColumn("productname")
mytable.Columns.Add(productidcolumn)
mytable.Columns.Add(productnamecolumn)
Dim ds As New Data.DataSet()
ds = getdataset()
Dim categoryid As Integer = 0
Dim expression As String = String.Empty
If e.Row.RowType = DataControlRowType.DataRow Then
categoryid = Int32.Parse(e.Row.Cells(0).Text)
expression = "categoryid = " & categoryid
Dim ddl As DropDownList = CType(e.Row.FindControl("dropdownlist1"), DropDownList)
Dim rows As Data.DataRow() = ds.Tables(0).Select(expression)
Dim row As Data.DataRow
For Each row In rows
Dim newrow As Data.DataRow = mytable.NewRow()
newrow("productid") = row("productid")
newrow("productname") = row("productname")
mytable.Rows.Add(newrow)
Next row
ddl.DataSource = mytable
ddl.DataTextField = "productname"
ddl.DataValueField = "productid"
ddl.DataBind()
End If
End Sub
|
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
Page_Load
|
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
gridview1.DataSource = getdataset().Tables(0)
gridview1.DataBind()
End Sub |
The front GridviewNestingDropdowlist.aspx page looks something like this:
<body>
<form id="form1" runat="server">
<fieldset>
<legend>GridviewNestingDropdowlist</legend>
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="False" onrowdatabound="gridview1_rowdatabound">
<columns>
<asp:boundfield datafield="categoryid" headertext="categoryid" />
<asp:boundfield datafield="categoryname" headertext="category name" />
<asp:BoundField DataField="productid" HeaderText="productid" />
<asp:templatefield headertext="products">
<itemtemplate>
<asp:dropdownlist id="dropdownlist1" runat="server">
</asp:dropdownlist>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview></fieldset>
</form>
</body>
|
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
The flow for the code behind page is as follows.
Imports System.Data.SqlClient
Partial Class GridviewNestingDropdowlist
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
gridview1.DataSource = getdataset().Tables(0)
gridview1.DataBind()
End Sub
Private Function getdataset() As Data.DataSet
Dim connectionstring As String = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password="
Dim query As String = "select p.categoryid,p.productid, p.productname,c.categoryid,c.categoryname from products p,categories c where p.categoryid=c.categoryid and c.categoryid<3"
Dim myconnection As New SqlConnection(connectionstring)
Dim ad As New SqlDataAdapter(query, myconnection)
Dim ds As New Data.DataSet()
ad.Fill(ds)
Return ds
End Function
Protected Sub gridview1_rowdatabound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridview1.RowDataBound
Dim mytable As New Data.DataTable()
Dim productidcolumn As New Data.DataColumn("productid")
Dim productnamecolumn As New Data.DataColumn("productname")
mytable.Columns.Add(productidcolumn)
mytable.Columns.Add(productnamecolumn)
Dim ds As New Data.DataSet()
ds = getdataset()
Dim categoryid As Integer = 0
Dim expression As String = String.Empty
If e.Row.RowType = DataControlRowType.DataRow Then
categoryid = Int32.Parse(e.Row.Cells(0).Text)
expression = "categoryid = " & categoryid
Dim ddl As DropDownList = CType(e.Row.FindControl("dropdownlist1"), DropDownList)
Dim rows As Data.DataRow() = ds.Tables(0).Select(expression)
Dim row As Data.DataRow
For Each row In rows
Dim newrow As Data.DataRow = mytable.NewRow()
newrow("productid") = row("productid")
newrow("productname") = row("productname")
mytable.Rows.Add(newrow)
Next row
ddl.DataSource = mytable
ddl.DataTextField = "productname"
ddl.DataValueField = "productid"
ddl.DataBind()
End If
End Sub
End Class
|
Download the Full Working Version of this Project written with Visual Studio.NET VB.NET 2005 Here!
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!