Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-模块/函数/VBA

(Repaste)gridview的修改,删除,更新,取消操作

时 间:2012-01-18 16:45:34
作 者:何必见戴   ID:13894  城市:厦门
摘 要:(Repaste)gridview的修改,删除,更新,取消操作
正 文:

数据库是采用sql server,语言c#,平台.net

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datamaster.aspx.cs" Inherits="datamaster" %>

<!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>
        &nbsp;<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
            GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <EditRowStyle BackColor="#2461BF" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>
        &nbsp;
        <br />
        <table style="width: 393px; height: 135px">
            <tr>
                <td style="width: 35px; height: 4px">
                    序号</td>
                <td style="height: 4px">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 35px; height: 20px">
                    标题</td>
                <td style="height: 20px">
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 35px; height: 9px">
                    内容</td>
                <td style="height: 9px">
                    <asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 35px; height: 8px">
                    日期</td>
                <td style="height: 8px">
                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 35px; height: 8px">
                    作者</td>
                <td style="height: 8px">
                    <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox></td>
            </tr>
        </table>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
    </form>
</body>
</html>

 

后台:

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 datamaster : System.Web.UI.Page
{
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    string strcon = "Data Source=.;Initial Catalog=mydata;User ID=sa;Password=123;Pooling=False";//数据库连接字符
    protected void Page_Load(object sender, EventArgs e)
    {

       if (!IsPostBack)
        {
            bind();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//编辑功能
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }


   public void bind()//绑定数据函数
    {
        string sqlstr = "select * from news";
        sqlcon = new SqlConnection(strcon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr,sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "pettale");
        GridView1.DataSource=myds ;
        GridView1.DataKeyNames = new string[] { "xuhao" };
        GridView1.DataBind();
        sqlcon.Close();
       

    }
    protected void Button1_Click(object sender, EventArgs e)//添加数据
    {
        string sqlstr = "insert into news(xuhao,biaoti,neirong,riqi,zuozhe) values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
        sqlcon = new SqlConnection(strcon);
        sqlcom = new SqlCommand(sqlstr,sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        bind();
      
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消编辑
    {
        GridView1.EditIndex = -1;
        bind();
    }
  
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//删除数据
    {
        string sqlstr = "delete from news where xuhao='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcon = new SqlConnection(strcon);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//更新数据
    {
        string sqlstr = "update news set xunhao = '"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',biaoti = '"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',neirong = '"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',riqi = '"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',zuozhe = '"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where xuhao='"+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        sqlcon = new SqlConnection(strcon);
        sqlcom = new SqlCommand(sqlstr, sqlcon);
        sqlcon.Open();
        sqlcom.ExecuteNonQuery();
        sqlcon.Close();
        GridView1.EditIndex = -1;
        bind();
    }


}



Access软件网官方交流QQ群 (群号:54525238)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助