In Asp.net 2.0, A dropdownlist can be nested in a gridview, which is very easy,
Different content is bound to each dropdownlist. For example, in the northwind database, you can use the gridview to display
Each category, and the category of each row can be listed in the drop-down box of dropdonwlist
Product. The following describes the implementation method.
The first is the page code.
<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 code above, we first bind the result to the gridview through a typical dataset. Note that there are two sentences in the SQL statement, and the first sentence is the return product, the second sentence is to return all categories of category, and when binding the gridview, we use
Gridview1.datasource = getdataset (). tables [1];, bind the category record in the first table to the gridview. Next, we need to process the dropdownlist in the template column, which can write code in the row_databound event, 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 create a datatable that includes the productid and productname columns, bind it to the gridview, and then use
Categoryid = int32.parse (E. Row. cells [0]. Text );
Expression = "categoryid =" + categoryid;
Construct a filter expression, which is specified as categoryid, and then
Dropdownlist DDL = (dropdownlist) E. Row. findcontrol ("dropdownlist1 ");
Datarow [] rows = Ds. Tables [0]. Select (expression );
To find all products whose category is equal to a certain categoryid. The datarow set is constructed here.
Add all the products under a certain category to the datatable, and finally bind the datatable and dropdownlist to implement the function.