GRIDVIEW對資料列編輯&刪除

摘要:GRIDVIEW對資料列編輯&刪除

//資料庫部分
USE [testDB]
 GO
 
 /****** Object:  Table [dbo].[userinfo]    Script Date: 12/02/2012 22:20:46 ******/
 SET ANSI_NULLS ON
 GO
 
 SET QUOTED_IDENTIFIER ON
 GO
 
 SET ANSI_PADDING ON
 GO
 
 CREATE TABLE [dbo].[userinfo](
 	[id] [int] IDENTITY(1,1) NOT NULL,
 	[username] [varchar](50) NULL,
 	[password] [varchar](50) NULL,
 	[birthday] [varchar](50) NULL,
  CONSTRAINT [PK_userinfo] PRIMARY KEY CLUSTERED 
 (
 	[id] ASC
 )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 ) ON [PRIMARY]
 
 GO
 
 SET ANSI_PADDING OFF
 GO


//aspx部分         
<asp:GridView ID="GridView1" runat="server"
  //以下兩行是產生編輯與刪除按鈕 
  AutoGenerateDeleteButton="True" 
  AutoGenerateEditButton="True" 
  DataKeyNames="id" 
  //以下四行為事件方法名稱
  onrowdeleting="GridView1_RowDeleting" 
  onrowediting="GridView1_RowEditing" 
  onrowupdating="GridView1_RowUpdating" 
  onrowcancelingedit="GridView1_RowCancelingEdit" >
</asp:GridView>

//cs部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
 
public partial class gridview_test : System.Web.UI.Page
{
    string Strcon = WebConfigurationManager.ConnectionStrings["test"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)  /*如果省略這句,下面的更新操作將無法完成,因为獲得的值是不變的*/
        {
            BindData();
        }
    }
 
    //把資料綁到GridView上
    private void BindData()
    {
        SqlConnection con = new SqlConnection(Strcon);
        String sql = "select userinfo.id,username,password,birthday from userinfo";
        SqlDataAdapter ad = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
 
    //刪除某列資料
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SqlConnection con = new SqlConnection(Strcon);
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*獲取主鍵,需要設置 DataKeyNames,這裏設为 id */
        String sql = "delete from userinfo where id='" + id + "'";
 
        SqlCommand com = new SqlCommand(sql, con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        BindData();
 
    }
 
    /*編輯操作,利用e.NewEditIndex獲取當前編輯行索引*/
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();              /*再次绑定顯示編輯行的原數據,不進行绑定要點2次編輯才能跳到編輯狀態*/
    }
 
 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection con = new SqlConnection(Strcon);
        String username = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text.ToString();    /*獲取要更新的數據*/
        String password = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text.ToString();
        String birthday = (GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text.ToString();
 
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);/*獲取主鍵,需要設置 DataKeyNames,這裏設为 id */
 
        String sql = "update userinfo set username='" + username + "',password='" + password + "',birthday='" + birthday + "' where id='" + id + "'";
 
        SqlCommand com = new SqlCommand(sql, con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        BindData();
    }
 
    //取消編輯
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;                 /*編輯索引賦值为-1,變回正常顯示狀態*/
        BindData();
    }
 
 

 

}