DataGrid里添加DropDownLit控件
2014-04-20 10:10
260 查看
Using DropDownList control in DataGrid
By 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)
{
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 method for Designer support - do not modify
/// the contents of this method with the code editor.
///
By 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)
{
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 method for Designer support - do not modify
/// the contents of this method with the code editor.
///
相关文章推荐
- 在DataGrid里添加DropDownLit控件【转贴】
- DataGrid控件-如何在DataGrid控件中添加CheckBox控件
- Windows 窗体 DataGrid 控件添加表和列
- DataGrid列头中添加CheckBox控件, HeaderSyle 在资源文件中,给模板中的控件添加事件
- 任意在datagrid里面添加控件。
- 在DataGrid页眉上添加全选的CheckBox控件
- 在ASP.NET控件DataGrid里添加确认删除的对话框
- 使用 Columns 集合动态向 DataGrid 控件添加列。
- 在DataGrid页眉上添加全选的CheckBox控件
- 为DataGrid添加CheckBox控件
- ASP.NET基础教程-DataGrid表格控件-利用ItemDataBound事件给表格第一列添加行号
- 为DataGrid添加CheckBox控件的例子
- 在DataGrid页眉上添加全选的CheckBox控件
- 框架 day49 BOS项目练习3(修复window控件BUG,添加/修改/作废取派员,datagrid使用,分页查询(DetachedCriteria离线),formatter函数)
- 控件之DataGrid----实现自动添加新行和数据交换
- 任意在datagrid里面添加控件。
- 将 ComboBox 控件添加到 Windows 窗体 DataGrid 控件
- 在DataGrid页眉上添加全选的CheckBox控件
- 为DataGrid添加CheckBox控件
- 在 DataGrid 的 Footer 中添加控件