Using an Edit Command Column in DataGrid

7 views
Skip to first unread message

yes4p...@gmail.com

unread,
May 17, 2006, 9:48:25 AM5/17/06
to PierceBBS
<%@ Page language="c#" Codebehind="fm2.aspx.cs" AutoEventWireup="false"
Inherits="WebTest.test.fm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>fm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout" style="FONT: 10pt verdana">
<form runat="server" ID="Form1">
<h3><font face="Verdana">Using an Edit Command Column in
DataGrid</font></h3>
<asp:DataGrid id="MyDataGrid" runat="server" BorderColor="black"
BorderWidth="1" CellPadding="3"
Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update" AutoGenerateColumns="false">
<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" ItemStyle-Wrap="false" HeaderText="Edit Command
Column"
HeaderStyle-Wrap="false" />
<asp:BoundColumn HeaderText="Item" ReadOnly="true"
DataField="Item" />
<asp:BoundColumn HeaderText="Quantity" DataField="Qty" />
<asp:BoundColumn HeaderText="Price" DataField="Price" />
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
---------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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 WebTest.test
{
/// <summary>
/// fm2 的摘要说明。
/// </summary>
public class fm2 : System.Web.UI.Page
{
DataTable Cart;
protected System.Web.UI.WebControls.DataGrid MyDataGrid;
DataView CartView;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(Session["DG6_ShoppingCart"]==null)
{
Cart = new DataTable();
Cart.Columns.Add(new DataColumn("Qty", typeof(string)));
Cart.Columns.Add(new DataColumn("Item", typeof(string)));
Cart.Columns.Add(new DataColumn("Price", typeof(string)));
Session["DG6_ShoppingCart"] = Cart;

// first load -- prepopulate with some data
for (int i=1; i<5; i++)
{
DataRow dr = Cart.NewRow();
dr[0] = ((int)(i%2)+1).ToString();
dr[1] = "Item " + i.ToString();
dr[2] = ((double)(1.23 * (i+1))).ToString();
Cart.Rows.Add(dr);
}
}
else
{
Cart = (DataTable)Session["DG6_ShoppingCart"];
}

CartView = new DataView(Cart);
CartView.Sort = "Item";
if (!IsPostBack)
{
BindGrid();
}
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs
e)
{
MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}

public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs
e)
{
MyDataGrid.EditItemIndex = -1;
BindGrid();
}

public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs
e)
{
// For bound columns the edited value is stored in a textbox,
// and the textbox is the 0th element in the column's cell
string item = e.Item.Cells[1].Text;
string qty = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
string price = ((TextBox)e.Item.Cells[3].Controls[0]).Text;

// with a database, we'd use an update command. Since we're using
an in-memory
// DataTable, we'll delete the old row and replace it with a new one
//remove old entry
CartView.RowFilter = "Item='"+item+"'";
if (CartView.Count > 0)
{
//item exists in cart
CartView.Delete(0);
}
CartView.RowFilter = "";

//add new entry
DataRow dr = Cart.NewRow();
dr[0] = qty;
dr[1] = item;
dr[2] = price;
Cart.Rows.Add(dr);

MyDataGrid.EditItemIndex = -1;
BindGrid();

}

public void BindGrid()
{
MyDataGrid.DataSource = CartView;
MyDataGrid.DataBind();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 -
不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


}
}

Reply all
Reply to author
Forward
0 new messages