DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Nesting the DropDownList to Gridview in ASP.NET 2.0(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 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;

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.


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;
}

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();
}

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


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();
}
}
}

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.


Looking for the VB.NET 2005 Version? 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!