asp.net中GridView实现查看详细信息,修改,删除数据

作者:袖梨 2022-06-25

信息管理实验7-使用GridView实现查看详细信息,修改,删除数据
首先创建好一个数据库&表
 代码如下 复制代码
CREATE DATABASE MedicineDB;
USE MedicineDB;
CREATE TABLE Medicine(
[ID] bigint,
[Name] varchar(50),
[Price] bigint,
[Type] varchar(50),
[Intro] text,
primary key(ID)
)

首先创建好一个数据库&表
 代码如下 复制代码
CREATE DATABASE MedicineDB;

USE MedicineDB;
CREATE TABLE Medicine(
	[ID] bigint,
	[Name] varchar(50),
	[Price] bigint,
	[Type] varchar(50),
	[Intro] text,
	primary key(ID)
);

USE MedicineDB;
INSERT INTO Medicine VALUES (1,'板蓝根','9','冲剂','清热解毒');
INSERT INTO Medicine VALUES (2,'999感冒颗粒','10','冲剂','感冒');
新建一个DB类
 代码如下 复制代码
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;

/// 
/// DB 的摘要说明
/// 
public class DB
{
	public DB()
	{

	}

    //创建数据库连接
    public static SqlConnection createCon(){
        return new SqlConnection("server=.sqlexpress;database=MedicineDB;uid=sa;pwd=123;");
    }

    //将数据绑定到GridView
    public static void gvDataBind(GridView gv,string sql){
        SqlConnection conn = DB.createCon();
        SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        gv.DataSource = ds;
        gv.DataBind();
        conn.Close();
    }

    //执行SQL语句
    public static void executeCmd(string sql) {
        SqlConnection conn = DB.createCon();
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql,conn);
        cmd.ExecuteNonQuery();
        conn.Close();
    }
}
界面设计


拉进一个GridView,在编辑列绑定数据(如图)并添加一个HyperLinkField(“详细”链接)和一个CommandField(“删除”链接),另外把GridView属性AutoGenerateColumns设为False



Default.aspx最终代码如下
 代码如下 复制代码
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>





    无标题页


    
Default.aspx.cs 代码
 代码如下 复制代码
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) bind();
    }

    public void bind() {
        string sql = "select * from Medicine";
        GridView1.DataKeyNames = new string[]{"ID"};
        DB.gvDataBind(GridView1, sql);
    }

    //记得绑定删除事件
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e){
        string sql = "delete from Medicine where ID = " + GridView1.DataKeys[e.RowIndex].Value;
        DB.executeCmd(sql);
        Response.Redirect("Default.aspx");
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            ((LinkButton)(e.Row.Cells[4].Controls[0])).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
        }
    }

}
注意:绑定UI的事件和代码关联,确保这里的参数和代码中的方法一致。

新建一个页面Show.aspx
Show.aspx代码如下
 代码如下 复制代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Show.aspx.cs" Inherits="Show" %>





    无标题页


    
ID
药品名
价格
类型
详细介绍

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

public partial class Show : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            string id = Request.QueryString["id"].ToString();
            string sql = "select * from Medicine where ID = " + id;
            SqlConnection conn = DB.createCon();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataSet ds = new DataSet();
            sda.Fill(ds,"Medicine");
            DataRowView drv = ds.Tables["Medicine"].DefaultView[0];
            TextBox1.Text = Convert.ToString(drv.Row["ID"]);
            TextBox2.Text = Convert.ToString(drv.Row["Name"]);
            TextBox3.Text = Convert.ToString(drv.Row["Price"]);
            TextBox4.Text = Convert.ToString(drv.Row["Type"]);
            TextBox5.Text = Convert.ToString(drv.Row["Intro"]);

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"].ToString();
        string sql = "update Medicine set [ID] = " + TextBox1.Text.Trim() + ",[Name] = '" + TextBox2.Text.Trim()
            +"',[Price] = "+TextBox3.Text.Trim()+",[Type] = '"+TextBox4.Text.Trim()+"',[Intro] = '"
            + TextBox5.Text.Trim() + "' where ID = " + id;
        DB.executeCmd(sql);
        Response.Redirect("Default.aspx");
    }
}
就这样完成了

相关文章

精彩推荐