再datagrid中使用droplist。。。。重要的是其中的几个用法

转载 2004年07月01日 10:04:00
再datagrid中使用droplist。。。。重要的是其中的几个用法

转贴:Andy.m   日期:2003-07-12   人气:33
在这里重要的不是要说明droplist再datagrid中的使用,而是主要看其中语句的几个特殊的用法,我会有注明的
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:

<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%#  GetCategory() %>"//****这里看下面的解释
DataTextField="categoryname"
DataValueField="categoryid"
SelectedIndex='<%# GetCategoryID((string)DataBinder.Eval(Container.DataItem, "categoryname")) %>' //***这里也看下面的解释
/>

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
//相信Productb是作者自定义的类
        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;//**看见sender的用法了吗?很多人都很奇怪这个得用处,这就是一个例子
            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.
        ///
        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 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 that 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.

再datagrid中使用droplist。。。。重要的是其中的几个用法

  • zgqtxwd
  • zgqtxwd
  • 2008年05月01日 03:46
  • 83

datagrid控件使用技巧大集合

引用自:http://www.cnblogs.com/iCeSnaker/archive/2004/07/31/29017.aspx  1、DataGrid的正反双向排序 http://dev.csd...
  • mengyao
  • mengyao
  • 2007年02月17日 08:49
  • 8488

WPF中DataGrid的使用

DataGrid是WPF中的数据显示控件,相当于Winform中的DataGridView。但是两者之间的用法确存在一些差异。在文档中,如果利用两者的不同之处可以更加清楚的描述好DataGrid的使用...
  • lsc19890723
  • lsc19890723
  • 2011年10月08日 21:02
  • 16325

easyui 中datagrid的使用方法

由于使用datagrid主要是和数据打交道,所以为了方便就用了js的方式创建数据表格 ...
  • jimscx
  • jimscx
  • 2015年06月03日 23:51
  • 1040

几个比较重要的设计模式的总结与应用

1. 设计模式分为创建型,结构型,行为型。   2. 创建型 2.1 工厂方法。生产同一接口的不同产品。 如:Spring Aop中AopProxyFactory(具体实现类为Defaul...
  • sum_rain
  • sum_rain
  • 2015年05月04日 22:24
  • 1515

WPF中DataGrid的简单用法

画一些简单的表使用DataGrid会比较方便,下面总结一下简单的使用DataGrid画个表格。这里写代码片 //这里是前台代码 ...
  • adl3135031
  • adl3135031
  • 2016年10月05日 18:06
  • 366

DataGrid的使用方法

Me.DataGrid1.AllowSorting = Not Me.DataGrid1.AllowSorting 得到或设置一个值,它指示是否可通过单及列标头对网格进行排序        Me.Da...
  • mustbelove
  • mustbelove
  • 2006年11月14日 08:37
  • 4919

在DataGrid里面使用CheckBox的例子

在DataGrid使用CheckBox通常都使用客户端的控件,这样可以把值帮定到CheckBox里面,在服务器端通过string strID = Request["cbID"];获得客户端所选择的值。...
  • conichiwa
  • conichiwa
  • 2004年10月16日 11:34
  • 810

easyui-datagrid_toolbar组件详细用法。

pageEncoding="UTF-8"%> http://java.sun.com/jstl/core_rt" prefix="c"%> http://java.sun.com/jsp/jstl...
  • u014369382
  • u014369382
  • 2014年11月06日 09:50
  • 718

DataGrid中的CheckBox处理

INPUT onclick="SelectDel(Form1)" type="checkbox" >全选function SelectDel(form)...{        var checked ...
  • enginetanmary
  • enginetanmary
  • 2007年07月05日 14:14
  • 543
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:再datagrid中使用droplist。。。。重要的是其中的几个用法
举报原因:
原因补充:

(最多只允许输入30个字)