datagrid| control?
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:
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 ()
{
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 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. Beca