在DataGrid中使用DropDownList控件

翻译|其它|编辑:郝浩|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  在任何情况下都不显示,因为一旦你设置了  EditItemIndexDataGrid  开始提取记录,同时, DropDownList  控件访问  GetCategory()方法取得数据源,如果  GetCategory()返回为空,将什么也得不到。

记住:设置  DataGrid    EditItemIndex  前,先设置控件数据源。

2.
  Product_Update()方法中,没有直接访问嵌入  DatGrid  中的  DropDownList  控件,取得控件的值是通过  FindControl()方法。该方法使用  DropDownList  控件的名称作为参数,返回查找到的  DropDownList  控件,以便能使用控件返回选择的值。

记住:使用  FindControl() 方法返回任何你想要在  DataGrid  中的任何控件,例如:TextBoxLabelCalendar  等。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:csdn

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP