Use the DROPDOWNLIST control in DataGrid

xiaoxiao2021-03-06  96

http://blog.9cbs.net/shoutor/archive/2004/07/02/32290.aspx

Use the DROPDOWNLIST control in DataGrid

A few days ago, when I developed a web application, I found some interesting things about the DataGrid control, I want to share with other vs.net programmers, so I wrote this article, this article demonstrates how DataGrid uses the DROPDOWNLIST control.

Below is the main part of the DropDown.aspx file:

DataSource = "<% # getcategory ()%>"

DataTextField = "categoryName"

DataValuefield = "categoryid"

SELECTEDINDEX = '<% # getcategoryId (String) DataBinder.eval (Container.DataItem, "categoryName")%>'

/>

In the second line, we set the data source of the DROPDOWNLIST control as a method getcatgory (), which gets a Category record in the database, returns the type DataTable. In the last line, we set the selectedIndex to method getCategoryId (), which returns the CategoryName position (integer), so that DropDownList displays the correct categoryName of the current record.

Here is the C # code:

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;

// Instantiate the object

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)

{

InitializationComponent ();

}

void bindproduct ()

{

/ / Return to the data table and bind to DataGrid

ProductGrid.DataSource = pdb.getProduct (); productGrid.database ();

}

Protected void product_ed (Object Sender, DataGridCommandeventArgs E)

{

Bindcategory ();

(DataGrid). TheedititeMindex = E.Item.itemindex;

Bindproduct ();

}

Protected Void Product_Cancel (Object Sender, DataGridCommandeventArgs E)

{

ProductGrid.editItemIndex = -1;

Bindproduct ();

}

Protected Void Product_Update (Object Sender, DataGridCommandeventArgs E)

{

//product name

String pname = E.Item.cell [1] .controls [0] .TEXT;

//product price

String price = E.Item.cell [2] .controls [0] .TEXT;

// categoryid

DropDownList DDL = (DropDownList) E.Item.cells [3] .findControl ("DropDownList1");

String categoryid = ddl.selecteditedItem.Value;

// productID

String pid = E.Item.cell [4] .controls [0] .TEXT;

// Call Update method to update data

PDB.Update (PID, PNAME, Price, CategoryID);

ProductGrid.editItemIndex = -1;

Bindproduct ();

}

void bindcategory ()

{

// Reversely back the data sheet

_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 vidinitiRizeComponent ()

{

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

}

}

C # file key

1. In the Product_Edit () method, you first have to call the bindcategory () method settings _category data table, then set the DataGrid's edititeMindex, finally, call the bindProduct () method. If you do not edit the order, DropDownList does not display under any circumstances, because once you set EditIndex, DataGrid starts to extract records, at the same time, the DROPDOWNLIST control accesses the getCateGory () method to get the data source, if getcategory () returns empty, what will be It is not possible.

Remember: Set the control data source before setting the DataGrid EditIndex. 2. In the Product_UPDATE () method, there is no direct access to the DROPDOWNLIST control embedded in the Datgrid, and the value of the acquisition is to pass the FindControl () method. This method uses the name of the DROPDOWNLIST control as a parameter, returns to the DropDownList control to be able to use the control to return the selected value.

Remember: Return any controls you want in DataGrid, such as TextBox, Label, Calendar, etc. you want to use the FindControl () method.

Published on July 02, 2004 12:34 PM

HREF = "/ shoutor / services / pingback.aspx" Rel = "pingback" />

comment

#

Reply: Use the DROPDOWNLIST control in DataGrid

2004-07-21 11:29 AM

LNT

Can you post the ASPX file?

#

Reply: Use the DROPDOWNLIST control in DataGrid

2004-07-21 5:32 PM

Native

The file is lost, it will be above

DataSource = "<% # getcategory ()%>"

DataTextField = "categoryName"

DataValuefield = "categoryid"

SELECTEDINDEX = '<% # getcategoryId (String) DataBinder.eval (Container.DataItem, "categoryName")%>'

/>

Place it in the template column

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

New Post(0)