GridView基本的操作 编辑 更新 取消

1.html代码
<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代码
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);
    }



    void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
string id = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();

        authorsbll.Delete(id);
//删除,函数自己写

        DataBindGridView();
//重新绑定数据源
    }


    void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        DataBindGridView();
    }


      void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        DataBindGridView();
    }


    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();
    }
    

    private void DataBindGridView()
    
{
        DataSet ds 
= authorsbll.GetList("");
        GridView1.DataSource 
= ds;
        GridView1.DataBind();
    }

}

posted @ 2008-09-20 17:16  大牛博客  阅读(342)  评论(0编辑  收藏  举报