Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Nesting the DropDownList to Gridview in ASP.NET 2.0(C#)
Nesting the DropDownList to Gridview in ASP.NET 2.0(C#)


ASP.NET Controls Tutorial

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 C#.

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.

using 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 DataSet getdataset()
{
string connectionstring = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password=";
string query = "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";
SqlConnection myconnection = new SqlConnection(connectionstring);
SqlDataAdapter ad = new SqlDataAdapter(query, myconnection);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}

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.

Binding DropDownList to GridView.

protected void gridview1_rowdatabound(object sender, GridViewRowEventArgs e)
{
DataTable mytable = new DataTable();
DataColumn productidcolumn = new DataColumn("productid");
DataColumn productnamecolumn = new DataColumn("productname");

mytable.Columns.Add(productidcolumn);
mytable.Columns.Add(productnamecolumn);

DataSet ds = new DataSet();
ds = getdataset();
int categoryid = 0;
string expression = string.Empty;

if (e.Row.RowType == DataControlRowType.DataRow)
{
categoryid = Int32.Parse(e.Row.Cells[0].Text);
expression = "categoryid = " + categoryid;
DropDownList ddl = (DropDownList)e.Row.FindControl("dropdownlist1");
DataRow[] rows = ds.Tables[0].Select(expression);

foreach (DataRow row in rows)
{
DataRow newrow = mytable.NewRow();
newrow["productid"] = row["productid"];
newrow["productname"] = row["productname"];
mytable.Rows.Add(newrow);
}
ddl.DataSource = mytable;
ddl.DataTextField = "productname";
ddl.DataValueField = "productid";
ddl.DataBind();
}
}

Page_Load

protected void Page_Load(object sender, EventArgs e)
{
gridview1.DataSource = getdataset().Tables[0];
gridview1.DataBind();
}

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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.

using System; using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridviewNestingDropdowlist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gridview1.DataSource = getdataset().Tables[0];
gridview1.DataBind();
}

private DataSet getdataset()
{
string connectionstring = "Data Source=localhost;Initial Catalog=northwind;User ID=sa;password=";
string query = "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";
SqlConnection myconnection = new SqlConnection(connectionstring);
SqlDataAdapter ad = new SqlDataAdapter(query, myconnection);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}

protected void gridview1_rowdatabound(object sender, GridViewRowEventArgs e)
{
DataTable mytable = new DataTable();
DataColumn productidcolumn = new DataColumn("productid");
DataColumn productnamecolumn = new DataColumn("productname");

mytable.Columns.Add(productidcolumn);
mytable.Columns.Add(productnamecolumn);

DataSet ds = new DataSet();
ds = getdataset();
int categoryid = 0;
string expression = string.Empty;

if (e.Row.RowType == DataControlRowType.DataRow)
{
categoryid = Int32.Parse(e.Row.Cells[0].Text);
expression = "categoryid = " + categoryid;
DropDownList ddl = (DropDownList)e.Row.FindControl("dropdownlist1");
DataRow[] rows = ds.Tables[0].Select(expression);

foreach (DataRow row in rows)
{
DataRow newrow = mytable.NewRow();
newrow["productid"] = row["productid"];
newrow["productname"] = row["productname"];
mytable.Rows.Add(newrow);
}
ddl.DataSource = mytable;
ddl.DataTextField = "productname";
ddl.DataValueField = "productid";
ddl.DataBind();
}
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!





 
  Developer Resources







Server Intellect Rocks