The DataGrid is important here not to illustrate the use of droplist in the DataGrid, but to look at a few of the special usages of the statements, which I would have noted.
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 ()%>"//**** here to see the explanations below
Datatextfield= "CategoryName"
Datavaluefield= "CategoryID"
Selectedindex= ' <%# Getcategoryid ((String) DataBinder.Eval (Container.DataItem, "CategoryName"))%> '//*** Here's a look at the following explanations
/>
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
Believe PRODUCTB is the author's custom class
Protected Productdb pdb=new productdb ();
Public DropDown ()
{
Page.Init + = new System.EventHandler (Page_Init);
}
Bindcategory ();
((DataGrid) sender). Did edititemindex=e.item.itemindex;//** see the use of sender? A lot of people are surprised by the usefulness of this, which is an example
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);
protected int Getcategoryid (string cname)
{
for (int i=0;i<_category. defaultview.count;i++)
{
if (_category. defaultview[i]["CategoryName"]. ToString () ==cname)
{
return i;
}
}
return 0;
}
#region Web Form Designer generated code
///
Required to Designer support-do not modify
The contents is with the Code Editor.
///
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}
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.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.