• 大小: 75KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C#
  • 标签: 增删改查  

资源简介

代码实现GridView的增删改查功能,适合于asp.net的初学者。

资源截图

代码片段和文件信息

using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    string StrConnection = ConfigurationManager.ConnectionStrings[“testconnectionstring“].ConnectionString.ToString();
    SqlConnection conn;
    SqlCommand cmd;
    protected void Page_Load(object sender EventArgs e)
    {
        if (!IsPostBack)
        {
            binddata();
        }
    }
    protected void binddata()
    {
        
        conn = new SqlConnection(StrConnection);
        conn.Open();
        String sql = “select * from People“;
        SqlDataAdapter myda = new SqlDataAdapter(sqlconn);
        DataSet ds = new DataSet();
        myda.Fill(ds “People“);
        GridView1.DataSource = ds.Tables[“People“].DefaultView;
        GridView1.DataBind();
        conn.Close();
    }
    protected void GridView1_RowDeleting(object sender GridViewDeleteEventArgs e)
    {
        
        conn = new SqlConnection(StrConnection);
        conn.Open();
        String id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        int idt = int.Parse(id);
        cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText= “delete from People where pkID = “ + idt;
        cmd.ExecuteNonQuery();
        conn.Close();
        binddata();
    }
    protected void Button1_Click(object sender EventArgs e)
    {
        Response.Redirect(“add.aspx“);
    }
    protected void GridView1_RowUpdating(object sender GridViewUpdateEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();

        string pkid = GridView1.Rows[e.RowIndex].Cells[2].Text.ToString();
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
        string sex = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();
        string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim();
        string born = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.Trim();
        string code = ((TextBox)GridView1.Rows[e.RowIndex].Cells[7].Controls[0]).Text.Trim();

        
        string SqlStr = “update People set pkID=‘“ + pkid + “‘Name=‘“ + name + “‘Sex=‘“ + sex + “‘Address=‘“ + address + “‘Born=‘“ + born + “‘Code=‘“ + code + “‘ where pkID=“ + pkid;
       
        conn = new SqlConnection(StrConnection);
        conn.Open();
        cmd = new SqlCommand(SqlStr conn);
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        conn.Close();
        GridView1.EditIndex = -1;
        binddata();
    }
    protected void GridView1_RowEditing(object sender GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIn

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件     1000960  2007-09-25 12:52  test
     文件        2585  2007-09-29 13:53  Default.aspx
     文件        5051  2007-09-29 13:41  Default.aspx.cs
     文件         492  2007-10-05 12:40  GridView.txt

评论

共有 条评论