欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net中使用DatagridView的增删改方法具体实现

程序员文章站 2024-03-01 09:41:10
default.aspx 页面: 复制代码 代码如下:<%@ page language="c#" autoeventwireup="true" codebehind...
default.aspx 页面:
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="gps_web.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>
        <!--gridview中必须写的几个事件:onrowediting、onrowupdating、onrowcancelingedit、onrowdeleting--->
        <asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"
            onpageindexchanging="gridview1_pageindexchanging" onrowcancelingedit="gridview1_rowcancelingedit"
            onrowediting="gridview1_rowediting" onrowupdating="gridview1_rowupdating"
            onrowdeleting="gridview1_rowdeleting"
            onselectedindexchanging="gridview1_selectedindexchanging">
            <columns>
                <asp:templatefield headertext="编号" visible="false">
                    <itemtemplate>
                        <asp:label id="lblnum" runat="server" text='<%# eval("num") %>'></asp:label>
                    </itemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="姓名">
                    <edititemtemplate>
                        <asp:textbox id="txttablename" runat="server" text='<%# eval("tablename") %>'></asp:textbox>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:label id="label2" runat="server" text='<%# eval("tablename") %>'></asp:label>
                    </itemtemplate>
                </asp:templatefield>
                <asp:templatefield headertext="地址">
                    <edititemtemplate>
                        <asp:textbox id="txttextname" runat="server" text='<%# eval("textname") %>'></asp:textbox>
                    </edititemtemplate>
                    <itemtemplate>
                        <asp:label id="label3" runat="server" text='<%# eval("textname") %>'></asp:label>
                    </itemtemplate>
                </asp:templatefield>
                <asp:commandfield headertext="状态" showselectbutton="true" />
                <asp:commandfield headertext="编辑" showeditbutton="true" />
                <asp:commandfield headertext="删除" showdeletebutton="true" />
            </columns>
        </asp:gridview>

        <br />
               
        <asp:button id="btnadd" runat="server" onclick="btnadd_click" text="添加" />
        <br />
        <br />   
    </div>
    </form>
</body>
</html>

default.aspx.cs页面代码:
复制代码 代码如下:

using system;
using system.web.ui;
using system.web.ui.webcontrols;
using gps_web.ywpages.dal;
using system.data;
namespace gps_web
{
    /// <summary>
    /// 参考的网址:http://blog.csdn.net/wanglei_samrtfish/article/details/8070480
    /// </summary>
    public partial class default : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {
            if (!page.ispostback)
            {
                gridviewbind();
            }
        }
        private void gridviewbind()
        {
            string sql = "select num,tablename,textname from dbo.grouptype_demo";
            try
            {     //绑定数据源
                gridview1.datasource = sqlhelper.executedataset(sqlhelper.getconnsting(), commandtype.text, sql).tables[0];
                gridview1.databind();
            }
            catch (exception ex){}
        }
        protected void gridview1_pageindexchanging(object sender, gridviewpageeventargs e)
        {
            gridview1.pageindex = e.newpageindex;
            gridviewbind();
        }
        protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e)
        {
            gridview1.editindex = -1;
            gridviewbind();
        }
        protected void gridview1_rowediting(object sender, gridviewediteventargs e)
        {  //gridview编辑项索引等于单击行的索引
            gridview1.editindex = e.neweditindex;
            gridviewbind();
        }
        /// <summary>
        /// 修改事件
        /// </summary>
        protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e)
        {  //取出编号
            int num = convert.toint32(((label)gridview1.rows[e.rowindex].findcontrol("lblnum")).text.tostring());
            //获取修改的值的内容
            string tablename = ((textbox)gridview1.rows[e.rowindex].findcontrol("txttablename")).text;
            string textname = ((textbox)gridview1.rows[e.rowindex].findcontrol("txttextname")).text;
            //更新记录
            string sql = string.format("update dbo.grouptype_demo set tablename='{0}',textname='{1}' where num={2}",tablename,textname,num);
            try
            {
                int i = sqlhelper.executenonquery(sqlhelper.getconnsting(), commandtype.text, sql);
                if (i > 0)
                {
                    clientscript.registerstartupscript(clientscript.gettype(), "myscript", "<script language='javascript'>alert('修改成功!')</script>");
                }
                else
                {
                    clientscript.registerstartupscript(clientscript.gettype(), "myscript", "<script language='javascript'>alert('修改失败!')</script>");
                }
            }
            catch (exception ex)
            {
                clientscript.registerstartupscript(clientscript.gettype(), "myscript", "<script language='javascript'>alert('操作无效!')</script>");
            }
            gridview1.editindex = -1;
            gridviewbind();
        }
        /// <summary>
        /// 删除事件
        /// </summary>
        protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e)
        {
            //取出编号
            int num = convert.toint32(((label)gridview1.rows[e.rowindex].findcontrol("lblnum")).text.tostring());
            //更新记录
            string sql = string.format("delete dbo.grouptype_demo where num={0}", num);
            try
            {
                int i = sqlhelper.executenonquery(sqlhelper.getconnsting(), commandtype.text, sql);
                if (i > 0)
                {
                    clientscript.registerstartupscript(clientscript.gettype(), "myscript", "<script language='javascript'>alert('删除成功!')</script>");
                }
                else
                {
                    clientscript.registerstartupscript(clientscript.gettype(), "myscript", "<script language='javascript'>alert('删除失败!')</script>");
                }
            }
            catch (exception ex)
            {
                clientscript.registerstartupscript(clientscript.gettype(), "myscript", "<script language='javascript'>alert('操作无效!')</script>");
            }  www.jb51.net
            gridview1.editindex = -1;
            gridviewbind();
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        protected void btnadd_click(object sender, eventargs e)
        {
            response.redirect("~/default_add.aspx");
        }
        protected void gridview1_selectedindexchanging(object sender, gridviewselecteventargs e)
        {
            //行号
            int i = e.newselectedindex;
            gridviewrow row = gridview1.rows[e.newselectedindex];

            btnadd.text = "你选中了第" + (i+1) + "行。";
        }       
    }
}

执行后的页面效果:
asp.net中使用DatagridView的增删改方法具体实现