信息管理实验7-使用GridView实现查看详细信息,修改,删除数据


信息管理实验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;

/// <summary>
/// DB 的摘要说明
/// </summary>
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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
            OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" />
                <asp:BoundField DataField="Name" HeaderText="药品名" />
                <asp:BoundField DataField="Price" HeaderText="价格" />
                <asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="Show.aspx?id={0}"
                    Text="详细" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>
</html>

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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        ID<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        药品名<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        价格<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        类型<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />
        详细介绍
        <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="保存" /></div>
    </form>
</body>
</html>

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

就这样完成了~原创文章,转载请保留出处(fatkun.com)




fatkun

17条评论

什么时候写完啊,很期待了。

fatkun 于 10/23/2009 4:33:23 PM 回复

这个是我实验课的实验~~自己做出来贴上的·~

发表评论

电子邮件地址不会被公开。