http://www.csharphelp.com/archives/archive212.html
?
Using DropdownList Control in DataGridby Eric ZHENG
When I was developing a web application couple days ago, I found some interesting things about the datagrid, I want to share them with other vs.net programmers, so I wrote this article. This article will demonstrate how to use DropDownList control in datagrid. The Essential Part of The Dropdown.aspx File Is The Following:
In second line, we set the datasource of 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 (an integer) of the categoryname, this enables the DorpDownList control to display the 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 is 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)
{
InitializationComponent ();
}
void bindproduct ()
{
//pdb.getProduct () Returns a DataTable to Product's DataGrid
ProductGrid.DataSource = pdb.getProduct ();
ProductGrid.databind ();
}
Protected void product_ed (Object Sender, DataGridCommandeventArgs E)
{
Bindcategory ();
(DataGrid). TheedititeMindex = 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.selecteditedItem.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 ()
{
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;
}
}
Return 0;
}
#Region Web Form Designer Generated Code
///
/// Required Method for Designer Support - Do Not Modify
/// The contents of this method with the code editor.
///
Private vidinitiRizeComponent () {
This.Load = New System.EventHandler (this.page_load);
}
#ndregion
}
}
The key points of this C # file are: 1.In Product_Edit () function, you have to call BindCategory () to set the _category datatable first, and then set the EditItemIndex for the datagrid, and at last, call BindProduct () function. The DropDownList control will not display anyting if you reverse this order. Because once you set the EditItemIndex, the datagrid begings rendering records, and at the same time, the DropDownList control access the function 'getCategory ()' to get the data source, if 'getCategory ()' returns nothing, you will not get anything of course Just remember:.. before setting EditItemIndex of datagrid, set the data source of the control 2.In Product_Update () function, you have no access to the DropDownList control directly which is embeded in the datagrid, the solution of getting the selected value of DropDownList control is the 'FindControl ()' function. This function takes the DropDownList control's name as its argument, and return the DropDownList control it found, so th At you can use the return control to get the selected value. Just Remember: Use FindControl () Function To Return ANY Control You Want to Find in The DataGrid, Such As Text Box, Text Area, Label, Calendar.