In asp.net 2.0, you can nest a dropdownlist in a gridview. This is very easy, and what we are talking about here is,
In each dropdownlist, different content is bound. For example, in the northwind database, you can use GRIDVIEW to display each category. At the same time, the category in each row can be listed in the form of a dropdonwlist drop-down box. All products under. The implementation method is introduced below.
The first is the code in the page part.
<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:TemplateField HeaderText="Products">
<ItemTemplate>
< asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In the codebehind section,
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// This is because Table[1] contains Categories
GridView1.DataSource = GetDataSet().Tables[1];
GridView1.DataBind();
}
}
private DataSet GetDataSet()
{
string query = @"SELECT p.CategoryID,p.ProductID, p.ProductName FROM Products p
SELECT c.CategoryID,c.CategoryName FROM Categories c";
string connectionString = "Server=localhost;Database=Northwind;user id=sa ;password=123456";
SqlConnection myConnection = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
return ds;
}
In the above code, first we return a typical dataset and bind it to the gridview. Note that there are two sentences in the SQL statement. The first sentence is to return the product, and the second sentence is to return all categories. In the binding When using gridview, we use
GridView1.DataSource = GetDataSet().Tables[1];, bind the category record in the first table table to the gridview. Next, we have to process the dropdownlist in the template column. This can be done in the row_databound event Write the code as follows
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();
}
}
The principle here is roughly as follows:
First, we created a datatable, including the productid and productname columns, and then bound it to the gridview, and then used
categoryID = Int32.Parse(e.Row.Cells[0].Text);
expression = "CategoryID = " + categoryID;
Construct a filter expression, here specified as categoryID, and then pass
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DataRow[] rows = ds.Tables[0].Select(expression);
, find all the products whose category is equal to a certain categoryID. Here, a datarow collection is constructed. Finally, use a loop
, add all products under a certain category to the datatable, and finally bind the datatable and dropdownlist to achieve the function.