Using DropdownList Control in DataGrid

xiaoxiao2021-03-06  109

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:

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 FunctionsProtected 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 ()

{

//pdb.getProduct () Returns a DataTable to Product's DataGrid

ProductGrid.DataSource = pdb.getProduct ();

ProductGrid.databind ();

}

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)

{

// 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.selecteditedItem.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 vidinitiRizeComponent ()

{

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

}

#ndregion

}

}

The Key Points of this C # file area:

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 EditIndex 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.

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

New Post(0)