Datagrid
Using DropDownList control in DataGrid
by Eric Zheng
When I is developing a Web application couple days ago, I found some interesting things about the DataGrid, I want to Sha Re them with the other vs.net programmers, so I wrote this article. This article would demonstrate how to use the DropDownList control in the DataGrid.
The essential part of the DropDown.aspx file is the following:
<asp:dropdownlist id= "DropDownList1" runat= "Server" datasource= <%# getcategory ()%> "datatextfield=" CategoryName "datavaluefield=" CategoryID "Selectedindex= ' <%# Getcategoryid ((String) DataBinder.Eval ( Container.DataItem, "CategoryName"))%> '/>
In second line, we set the DataSource's the DropDownList control to a function ' getcategory () ', this function fetches the Category records from Database and returns a DataTable. In the last line, we set the "SelectedIndex to a" Funciton ' Getcategoryid ', this function takes the current CategoryName as Its argument, and returns the Locaiton (a integer) of the CategoryName, this enables the Dorpdownlist control to display T He correct CategoryName for the current record.
The following is the C # code:
Using system;using system.collections;using system.componentmodel;using system.data;using System.Data.OleDb;using System.configuration;using system.drawing;using system.web;using system.web.sessionstate;using System.Web.UI;using System.web.ui.webcontrols;using System.web.ui.htmlcontrols;namespace management{ public class dropdown:system.web.ui.page { protected System.Web.UI.WebControls.DataGrid productgrid; protected DataTable _category; //new a database class to get records, Productdb are a public class //containing Several functions protected productdb pdb=new ProductDb (); public DropDown () { Page.Init + = new System.EventHandler (Page_Init); } private void Page_Load (object sender, System.EventArgs e) { if (! IsPostBack) { bindproduct (); } } private void Page_Init (object sender, EventArgs e) { initializecomponent (); } void BindProduct () { //pdb. GetProduct () returns a DataTable to Product ' s datagrid productgrid.datasource=pdb. GetProduct (); productgrid.databind (); } protected void Product_edit (object sender, DataGridCommandEventArgs e) { bindcategory (); ((DataGrid) sender). edititemindex=e.item.itemindex; Bindproduct (); } protected void Product_cancel (object sender, DataGridCommandEventArgs e) { productgrid.edititemindex =-1; bindproduct (); } protected void Product_Update ( Object sender, DataGridCommandEventArgs e) { //get the Currnet product name string Pname=e.item.cell[1]. ControLs[0]. Text; //get the current product price string price=e.item.cell[2]. Controls[0]. Text; //get the current categoryid dropdownlist ddl= (DropDownList) e.item.cells[3]. FindControl ("DropDownList1"); string Categoryid=ddl. Selecteditem.value; //get the current Productid string Pid=e.Item.Cell[4]. Controls[0]. Text; //call pdb ' s update function pdb.update (Pid,pname,price,categoryid); ProductGrid.EditItemIndex=-1; Bindproduct (); } void BindCategory () { //pdb. Fetchcategory () returns a datatable _ Category=pdb. Fetchcategory (); } protected DataTable getcategory () { &nbSp; return _category; } protected int Getcategoryid (string cname) { for (int i=0;i<_category. defaultview.count;i++) { if (_category. defaultview[i]["CategoryName"]. ToString () ==cname) { return i; } } &nBsp; return 0; } #region Web Form Designer generated code /// ///Required method to Designer support-do not modify ///the contents of this method with the Code editor. /// private void InitializeComponent () { this. Load + = new System.EventHandler (this. Page_Load); } # Endregion&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}}
The key points of this C # file are:
1.In Product_edit () function, you have to call Bindcategory () to set the _category DataTable, and then set the Editi Temindex for the "DataGrid", and at last, call Bindproduct () function. The DropDownList control won't display anyting If you reverse this order. Because once you are set the EditItemIndex, the DataGrid begings rendering records, and at the same time, the DropDownList con Trol access the function ' getcategory () ' To get the data source, if ' getcategory () ' returns nothing and you'll not get anyt Hing of course. Just Remember:before setting EditItemIndex of the DataGrid, set the data source of the control.
2.In product_update () function, have no access to the DropDownList control directly which are embeded in the DataGrid, The solution of getting the selected value of DropDownList control is the ' FindControl () ' function. This function takes the "DropDownList control", the name as its argument, and return the DropDownList control it found You can use the ' return control ' to the selected value. Just remember:use FindControl () function to return no control for you want to find in the DataGrid, such as text box, text a REA, label, Calendar.