To use dropdownlist in the DataGrid, you must use
1. Datagrid_itemdatabound (Object sender, system. Web. UI. webcontrols. datagriditemeventargs e) event of the DataGrid
Itemdataboundz occurs when the item is bound to the DataGrid Control
2. Listtypeitem Enumeration
Specifies the type of items in the list control.
| member name |
description |
| alternatingitem |
alternating (an even index starting from scratch) items in cells. It is bound to Data. |
| edititem |
items in edit mode in the list control. It is bound to Data. |
| footer |
footer of the list control. It is not bound to Data. |
| header |
the header of the list control. It is not bound to Data. |
| item |
items in the list control. It is bound to Data. |
| pager |
page navigation: displays the controls that are located on different pages associated with the DataGrid Control. It is not bound to Data. |
| selecteditem |
selected items in the list control. It is bound to Data. |
| separator |
delimiter between items in the list control. It is not bound to Data. |
Example:
ASPX page --- only returns the DataGrid part of the page
<Asp: DataGrid id = "mydatagrid" runat = "server">
<Columns>
<Asp: templatecolumn headertext = "transaction">
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "transaction") %>
</Itemtemplate>
<Edititemtemplate>
<Asp: dropdownlist id = dropdown runat = "server"> </ASP: dropdownlist> // The tag of dropdownlist determines the selection of the listtypeitem to be bound to Data.
</Edititemtemplate>
</ASP: templatecolumn>
</Columns> <br> </ASP: DataGrid> </P>
. CS File
Private void mydatagrid_itemdatabound (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
If (E. Item. itemtype = listitemtype. edititem )? // If"The instance where the object reference is not set to the objectThe listitemtype is incorrect.
{
Sqlconnection conn = new sqlconnection ("database = test; uid = sa; PWD; server = (local)");
Sqldataadapter da = new sqldataadapter ("select * from users", Conn );
Dataset DS = new dataset ();
Da. Fill (DS );
Dropdownlist drop = (dropdownlist) E. Item. findcontrol ("dropdown");
Drop. datatextfield = "ID ";
Drop. datavaluefield = "cusname ";
Drop. datasource = Ds. Tables [0]. defaultview;
Drop. databind ();
}
}