<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView基本的操作 编辑 更新 取消</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone" />
<asp:BoundField DataField="address" HeaderText="address" />
<asp:CommandField ShowEditButton="True" HeaderText="编辑" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
2.cs代码<head runat="server">
<title>GridView基本的操作 编辑 更新 取消</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="au_id" HeaderText="au_id" />
<asp:BoundField DataField="au_lname" HeaderText="au_lname" />
<asp:BoundField DataField="au_fname" HeaderText="au_fname" />
<asp:BoundField DataField="phone" HeaderText="phone" />
<asp:BoundField DataField="address" HeaderText="address" />
<asp:CommandField ShowEditButton="True" HeaderText="编辑" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridView_GridView_DeleteUpdate1 : System.Web.UI.Page
{
Practice.DAL.authors authorsbll = new Practice.DAL.authors();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindGridView();
}
GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
}
/// <summary>
/// 功 能:GridView删除事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();
authorsbll.Delete(id);//删除,函数自己写
DataBindGridView();//重新绑定数据源
}
/// <summary>
/// 功 能:GridView编辑事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataBindGridView();
}
/// <summary>
/// 功 能:GridView取消事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataBindGridView();
}
/// <summary>
/// 功 能:GridView更新事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string aa = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
GridView1.EditIndex = -1;
DataBindGridView();
}
/// <summary>
/// 功 能:绑定GridView
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
private void DataBindGridView()
{
DataSet ds = authorsbll.GetList("");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridView_GridView_DeleteUpdate1 : System.Web.UI.Page
{
Practice.DAL.authors authorsbll = new Practice.DAL.authors();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindGridView();
}
GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
GridView1.RowEditing += new GridViewEditEventHandler(GridView1_RowEditing);
GridView1.RowCancelingEdit += new GridViewCancelEditEventHandler(GridView1_RowCancelingEdit);
GridView1.RowUpdating += new GridViewUpdateEventHandler(GridView1_RowUpdating);
}
/// <summary>
/// 功 能:GridView删除事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();
authorsbll.Delete(id);//删除,函数自己写
DataBindGridView();//重新绑定数据源
}
/// <summary>
/// 功 能:GridView编辑事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataBindGridView();
}
/// <summary>
/// 功 能:GridView取消事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataBindGridView();
}
/// <summary>
/// 功 能:GridView更新事件
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string aa = (((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim());
GridView1.EditIndex = -1;
DataBindGridView();
}
/// <summary>
/// 功 能:绑定GridView
/// 作 者:PUKE
/// 完成时间:2007-05-18
/// 版 权:pukesys@tom.com
/// </summary>
private void DataBindGridView()
{
DataSet ds = authorsbll.GetList("");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}