DotNet Tutorials

Server Intellect

 Nesting the DropDownList to Gridview in ASP.NET 2.0(VB)

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

Try Server Intellect for Windows Server Hosting. Quality and Quantity!


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

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

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

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.


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>

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

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.


Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 
123 ASP

411 ASP

Dot Net Freaks

Server Intellect