没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|其它|编辑:郝浩|2007-08-17 10:24:28.000|阅读 1878 次
概述:
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
几天前,当我在开发一个 Web 应用程序时,我发现了关于 DataGrid 控件的一些有趣的事情,我想与其他 VS.Net 程序员共享,因此我写这篇文章,这篇文章演示如何在 DataGrid 中使用 DropDownList 控件。
下面是 DropDown.aspx 文件的主要部分:
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%# GetCategory() %>"
DataTextField="categoryname"
DataValueField="categoryid"
SelectedIndex='<%# GetCategoryID((string)DataBinder.Eval(Container.DataItem, "categoryname")) %>'
/>
在第二行,我们设置 DropDownList 控件的数据源为方法 GetCatgory(),该方法从数据库中获取 Category 记录,返回类型为 DataTable。在最后一行,我们设置 SelectedIndex 为方法 GetCategoryID(),该函数以当前的 CategoryName 作为参数,返回 CategoryName 的位置(整数),使 DropDownList 显示当前记录的正确 CategoryName.
下面是 C# 代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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;
// 实例化对象
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()
{
//返回数据表,并绑定到 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)
{
//产品名称
string pname=e.Item.Cell[1].Controls[0].Text;
//产品价格
string price=e.Item.Cell[2].Controls[0].Text;
//categoryid
DropDownList ddl=(DropDownList)e.Item.Cells[3].FindControl("DropDownList1");
string categoryid=ddl.SelectedItem.Value;
//productid
string pid=e.Item.Cell[4].Controls[0].Text;
//调用update方法更新数据
pdb.update(pid,pname,price,categoryid);
ProductGrid.EditItemIndex=-1;
BindProduct();
}
void BindCategory()
{
//反回数据表
_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
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
C#文件的关键点
1.在 Product_Edit()方法中,你先得调用 BindCategory()方法设置_category 数据表,然后设置 DataGrid 的 EditItemIndex,最后,调用 BindProduct()方法。如果你不编辑订单,DropDownList 在任何情况下都不显示,因为一旦你设置了 EditItemIndex,DataGrid 开始提取记录,同时, DropDownList 控件访问 GetCategory()方法取得数据源,如果 GetCategory()返回为空,将什么也得不到。
记住:设置 DataGrid 的 EditItemIndex 前,先设置控件数据源。
2.在 Product_Update()方法中,没有直接访问嵌入 DatGrid 中的 DropDownList 控件,取得控件的值是通过 FindControl()方法。该方法使用 DropDownList 控件的名称作为参数,返回查找到的 DropDownList 控件,以便能使用控件返回选择的值。
记住:使用 FindControl() 方法返回任何你想要在 DataGrid 中的任何控件,例如:TextBox、Label、Calendar 等。
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:csdn面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号