DataGrid.EditCommand 事件

本文展示了如何使用ASP.NET DataGrid控件实现数据编辑功能。包括启用编辑、取消编辑及更新数据源的过程。通过实例代码详细介绍了如何响应各种事件,如EditCommand、CancelCommand和UpdateCommand等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.datagrid.editcommand(VS.80).aspx

None.gif <% @ Page Language = " C# "  AutoEventWireup = " True "   %>
None.gif
<% @ Import Namespace = " System.Data "   %>
None.gif 
None.gif
< html >
None.gif   
< script runat = " server " >
None.gif 
None.gif      
//  The Cart and CartView objects temporarily store the data source
None.gif      
//  for the DataGrid control while the page is being processed.
None.gif
      DataTable Cart  =   new  DataTable();
None.gif      DataView CartView;   
None.gif 
None.gif      
void  Page_Load(Object sender, EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif 
InBlock.gif         
// With a database, use an select query to retrieve the data. Because 
InBlock.gif         
// the data source in this example is an in-memory DataTable, retrieve
InBlock.gif         
// the data from session state if it exists; otherwise, create the data
InBlock.gif         
// source.
InBlock.gif
         GetSource();
InBlock.gif
InBlock.gif         
// The DataGrid control maintains state between posts to the server;
InBlock.gif         
// it only needs to be bound to a data source the first time the page
InBlock.gif         
// is loaded or when the data source is updated.
InBlock.gif
         if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif            BindGrid();
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif                   
ExpandedBlockEnd.gif      }

None.gif 
None.gif      
void  ItemsGrid_Edit(Object sender, DataGridCommandEventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif
InBlock.gif         
// Set the EditItemIndex property to the index of the item clicked 
InBlock.gif         
// in the DataGrid control to enable editing for that item. Be sure
InBlock.gif         
// to rebind the DateGrid to the data source to refresh the control.
InBlock.gif
         ItemsGrid.EditItemIndex = e.Item.ItemIndex;
InBlock.gif         BindGrid();
InBlock.gif
ExpandedBlockEnd.gif      }

None.gif 
None.gif      
void  ItemsGrid_Cancel(Object sender, DataGridCommandEventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif
InBlock.gif         
// Set the EditItemIndex property to -1 to exit editing mode. 
InBlock.gif         
// Be sure to rebind the DateGrid to the data source to refresh
InBlock.gif         
// the control.
InBlock.gif
         ItemsGrid.EditItemIndex = -1;
InBlock.gif         BindGrid();
InBlock.gif
ExpandedBlockEnd.gif      }

None.gif 
None.gif      
void  ItemsGrid_Update(Object sender, DataGridCommandEventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif
InBlock.gif         
// Retrieve the text boxes that contain the values to update.
InBlock.gif         
// For bound columns, the edited value is stored in a TextBox.
InBlock.gif         
// The TextBox is the 0th control in a cell's Controls collection.
InBlock.gif         
// Each cell in the Cells collection of a DataGrid item represents
InBlock.gif         
// a column in the DataGrid control.
InBlock.gif
         TextBox qtyText = (TextBox)e.Item.Cells[3].Controls[0];
InBlock.gif         TextBox priceText 
= (TextBox)e.Item.Cells[4].Controls[0];
InBlock.gif 
InBlock.gif         
// Retrieve the updated values.
InBlock.gif
         String item = e.Item.Cells[2].Text;
InBlock.gif         String qty 
= qtyText.Text;
InBlock.gif         String price 
= priceText.Text;
InBlock.gif        
InBlock.gif         DataRow dr;
InBlock.gif 
InBlock.gif         
// With a database, use an update command to update the data. 
InBlock.gif         
// Because the data source in this example is an in-memory 
InBlock.gif         
// DataTable, delete the old row and replace it with a new one.
InBlock.gif 
InBlock.gif         
// Remove the old entry and clear the row filter.
InBlock.gif
         CartView.RowFilter = "Item='" + item + "'";
InBlock.gif         
if (CartView.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            CartView.Delete(
0);
ExpandedSubBlockEnd.gif         }

InBlock.gif         CartView.RowFilter 
= "";
InBlock.gif 
InBlock.gif         
// ***************************************************************
InBlock.gif         
// Insert data validation code here. Be sure to validate the
InBlock.gif         
// values entered by the user before converting to the appropriate
InBlock.gif         
// data types and updating the data source.
InBlock.gif         
// ***************************************************************
InBlock.gif
InBlock.gif         
// Add the new entry.
InBlock.gif
         dr = Cart.NewRow();
InBlock.gif         dr[
0= Convert.ToInt32(qty);
InBlock.gif         dr[
1= item;
InBlock.gif
InBlock.gif         
// If necessary, remove the '$' character from the price before 
InBlock.gif         
// converting it to a Double.
InBlock.gif
         if(price[0== '$')
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            dr[
2= Convert.ToDouble(price.Substring(1));
ExpandedSubBlockEnd.gif         }

InBlock.gif         
else
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            dr[
2= Convert.ToDouble(price);
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         Cart.Rows.Add(dr);
InBlock.gif 
InBlock.gif         
// Set the EditItemIndex property to -1 to exit editing mode. 
InBlock.gif         
// Be sure to rebind the DateGrid to the data source to refresh
InBlock.gif         
// the control.
InBlock.gif
         ItemsGrid.EditItemIndex = -1;
InBlock.gif         BindGrid();
InBlock.gif
ExpandedBlockEnd.gif      }

None.gif 
None.gif      
void  BindGrid() 
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif
InBlock.gif         
// Set the data source and bind to the Data Grid control.
InBlock.gif
         ItemsGrid.DataSource = CartView;
InBlock.gif         ItemsGrid.DataBind();
InBlock.gif
ExpandedBlockEnd.gif      }

None.gif
None.gif      
void  GetSource()
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif
InBlock.gif         
// For this example, the data source is a DataTable that is stored
InBlock.gif         
// in session state. If the data source does not exist, create it;
InBlock.gif         
//  otherwise, load the data.
InBlock.gif
         if (Session["ShoppingCart"== null
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{     
InBlock.gif
InBlock.gif            
// Create the sample data.
InBlock.gif
            DataRow dr;  
InBlock.gif 
InBlock.gif            
// Define the columns of the table.
InBlock.gif
            Cart.Columns.Add(new DataColumn("Qty"typeof(Int32)));
InBlock.gif            Cart.Columns.Add(
new DataColumn("Item"typeof(String)));
InBlock.gif            Cart.Columns.Add(
new DataColumn("Price"typeof(Double)));
InBlock.gif
InBlock.gif            
// Store the table in session state to persist its values 
InBlock.gif            
// between posts to the server.
InBlock.gif
            Session["ShoppingCart"= Cart;
InBlock.gif             
InBlock.gif            
// Populate the DataTable with sample data.
InBlock.gif
            for (int i = 1; i <= 9; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif               dr 
= Cart.NewRow();
InBlock.gif               
if (i % 2 != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                  dr[
0= 2;
ExpandedSubBlockEnd.gif               }

InBlock.gif               
else
ExpandedSubBlockStart.gifContractedSubBlock.gif               
dot.gif{
InBlock.gif                  dr[
0= 1;
ExpandedSubBlockEnd.gif               }

InBlock.gif               dr[
1= "Item " + i.ToString();
InBlock.gif               dr[
2= (1.23 * (i + 1));
InBlock.gif               Cart.Rows.Add(dr);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif         }
 
InBlock.gif
InBlock.gif         
else
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif            
// Retrieve the sample data from session state.
InBlock.gif
            Cart = (DataTable)Session["ShoppingCart"];
InBlock.gif
ExpandedSubBlockEnd.gif         }
         
InBlock.gif 
InBlock.gif         
// Create a DataView and specify the field to sort by.
InBlock.gif
         CartView = new DataView(Cart);
InBlock.gif         CartView.Sort
="Item";
InBlock.gif
InBlock.gif         
return;
InBlock.gif
ExpandedBlockEnd.gif      }

None.gif
None.gif      
void  ItemsGrid_Command(Object sender, DataGridCommandEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif
InBlock.gif         
switch(((LinkButton)e.CommandSource).CommandName)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif            
case "Delete":
InBlock.gif               DeleteItem(e);
InBlock.gif               
break;
InBlock.gif
InBlock.gif            
// Add other cases here, if there are multiple ButtonColumns in 
InBlock.gif            
// the DataGrid control.
InBlock.gif

InBlock.gif            
default:
InBlock.gif               
// Do nothing.
InBlock.gif
               break;
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
ExpandedBlockEnd.gif      }

None.gif
None.gif      
void  DeleteItem(DataGridCommandEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif
InBlock.gif         
// e.Item is the table row where the command is raised. For bound
InBlock.gif         
// columns, the value is stored in the Text property of a TableCell.
InBlock.gif
         TableCell itemCell = e.Item.Cells[2];
InBlock.gif         
string item = itemCell.Text;
InBlock.gif
InBlock.gif         
// Remove the selected item from the data source.         
InBlock.gif
         CartView.RowFilter = "Item='" + item + "'";
InBlock.gif         
if (CartView.Count > 0
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{     
InBlock.gif            CartView.Delete(
0);
ExpandedSubBlockEnd.gif         }

InBlock.gif         CartView.RowFilter 
= "";
InBlock.gif
InBlock.gif         
// Rebind the data source to refresh the DataGrid control.
InBlock.gif
         BindGrid();
InBlock.gif
ExpandedBlockEnd.gif      }

None.gif 
None.gif   
</ script >
None.gif 
None.gif
< body >
None.gif 
None.gif   
< form runat = " server " >
None.gif
None.gif      
< h3 > DataGrid Editing Example </ h3 >
None.gif 
None.gif      
< asp:DataGrid id = " ItemsGrid "
None.gif           BorderColor
= " black "
None.gif           BorderWidth
= " 1 "
None.gif           CellPadding
= " 3 "
None.gif           OnEditCommand
= " ItemsGrid_Edit "
None.gif           OnCancelCommand
= " ItemsGrid_Cancel "
None.gif           OnUpdateCommand
= " ItemsGrid_Update "
None.gif           OnItemCommand
= " ItemsGrid_Command "
None.gif           AutoGenerateColumns
= " false "
None.gif           runat
= " server " >
None.gif
None.gif         
< HeaderStyle BackColor = " #aaaadd " >
None.gif         
</ HeaderStyle >
None.gif 
None.gif         
< Columns >
None.gif
None.gif            
< asp:EditCommandColumn
None.gif                 EditText
= " Edit "
None.gif                 CancelText
= " Cancel "
None.gif                 UpdateText
= " Update "  
None.gif                 HeaderText
= " Edit item " >
None.gif
None.gif               
< ItemStyle Wrap = " False " >
None.gif               
</ ItemStyle >
None.gif
None.gif               
< HeaderStyle Wrap = " False " >
None.gif               
</ HeaderStyle >
None.gif
None.gif            
</ asp:EditCommandColumn >
None.gif
None.gif            
< asp:ButtonColumn 
None.gif                 HeaderText
= " Delete item "  
None.gif                 ButtonType
= " LinkButton "  
None.gif                 Text
= " Delete "  
None.gif                 CommandName
= " Delete " />   
None.gif 
None.gif            
< asp:BoundColumn HeaderText = " Item "  
None.gif                 ReadOnly
= " True "  
None.gif                 DataField
= " Item " />
None.gif 
None.gif            
< asp:BoundColumn HeaderText = " Quantity "  
None.gif                 DataField
= " Qty " />
None.gif 
None.gif            
< asp:BoundColumn HeaderText = " Price "
None.gif                 DataField
= " Price "
None.gif                 DataFormatString
= " {0:c} " />
None.gif 
None.gif         
</ Columns >
None.gif 
None.gif      
</ asp:DataGrid >
None.gif
None.gif   
</ form >
None.gif 
None.gif
</ body >
None.gif
</ html >

转载于:https://www.cnblogs.com/netroot/archive/2007/08/17/859444.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值