Use Datagrid to use DROPLIST. . . . What's important is one of the usage

zhaozj2021-02-16  54

Use Datagrid to use DROPLIST. . . .

Important is one of them to repumented: Andy.M Date: 2003-07-12 Popularity: 33 It is important to explain that DROPLIST is used in DataGrid, but mainly to see several special usage of the statement, I have stated Using DropDownList control in DataGridBy Eric ZhengWhen 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', thi s 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 It is believed Productb functions // custom class of 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.Database ();} protected void product_edit (OB JECT Sender, DataGridCommandeventArgs E) {bindcategory (); (dataGrid) sender) .edititemindex = E.Item.itemindex; // ** See Sender's usage? Many people have to use this very surprising, this is an example 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.editIndex = -1; bindproduct ();} void bindcategory ()} 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);

转载请注明原文地址:https://www.9cbs.com/read-18493.html

New Post(0)