欢迎光临
我们一直在努力

使用单击和双击更新DataGrid中的数据的例子-.NET教程,数据库应用

建站超值云服务器,限时71元/月

 

可能大家在使用datagrid编辑数据的时候会觉得要点击最后的编辑列的按钮来修改数据不是很方便,在今天的例子中给出一种比较方便的操作方法:要修改数据点击这一行-》鼠标放在哪个文本框就会选中哪个文本框的内容,直接可以进行修改-》修改完毕以后双击这一行(也可以直接双击文本框)进行保存。这次使用sqlserver数据库中的northwind数据库中的employees表作为例子。

前台:

<%@ page language=”c#” codebehind=”webform87.aspx.cs” autoeventwireup=”false” inherits=”csdn.webform87″ %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
    <head>
        <title>webform87</title>
        <meta content=”microsoft visual studio .net 7.1″ name=”generator”>
        <meta content=”c#” name=”code_language”>
        <meta content=”javascript” name=”vs_defaultclientscript”>
        <meta content=”http://schemas.microsoft.com/intellisense/ie5″ name=”vs_targetschema”>
        <link href=”css.css” type=”text/css” rel=”stylesheet”>
    </head>
    <body>
        <form id=”form1″ method=”post” runat=”server”>
            <asp:datagrid id=”datagrid1″ runat=”server” autogeneratecolumns=”false” cellspacing=”1″ borderwidth=”0px”
                cellpadding=”5″ cssclass=”border” datakeyfield=”employeeid”>
                <itemstyle cssclass=”item”></itemstyle>
                <headerstyle cssclass=”header”></headerstyle>
                <columns>
                    <asp:boundcolumn datafield=”firstname” headertext=”firstname”></asp:boundcolumn>
                    <asp:boundcolumn datafield=”lastname” headertext=”lastname”></asp:boundcolumn>
                    <asp:boundcolumn datafield=”title” headertext=”title”></asp:boundcolumn>
                    <asp:boundcolumn datafield=”birthdate” headertext=”birthdate” dataformatstring=”{0:yyyy-mm-dd}”></asp:boundcolumn>
                    <asp:buttoncolumn buttontype=”linkbutton” commandname=”edit” text=”edit” visible=”false”></asp:buttoncolumn>
                    <asp:buttoncolumn buttontype=”linkbutton” commandname=”update” text=”update” visible=”false”></asp:buttoncolumn>
                </columns>
            </asp:datagrid>
        </form>
    </body>
</html>

后台:

using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.data.sqlclient;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;

namespace csdn
{
    public class webform87 : system.web.ui.page
    {
        protected system.web.ui.webcontrols.datagrid datagrid1;
   
        private void page_load(object sender, system.eventargs e)
        {
            if(!ispostback)
            {
                setbind();
            }
        }

        private void setbind()
        {
            sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“strconn”]);
            sqldataadapter da=new sqldataadapter(“select * from employees”,conn);
            dataset ds=new dataset();
            da.fill(ds,”table1″);
            this.datagrid1.datasource=ds.tables[“table1”];
            this.datagrid1.databind();           
        }

        #region web 窗体设计器生成的代码
        override protected void oninit(eventargs e)
        {
            //
            // codegen: 该调用是 asp.net web 窗体设计器所必需的。
            //
            initializecomponent();
            base.oninit(e);
        }
       
        /// <summary>
        /// 设计器支持所需的方法 – 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void initializecomponent()
        {   
            this.datagrid1.itemcommand += new system.web.ui.webcontrols.datagridcommandeventhandler(this.datagrid1_itemcommand);
            this.datagrid1.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.datagrid1_itemdatabound);
            this.load += new system.eventhandler(this.page_load);

        }
        #endregion

        private void datagrid1_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
        {
            if(e.item.itemtype==listitemtype.item||e.item.itemtype==listitemtype.alternatingitem)
            {
                e.item.attributes.add(“onclick”,”__dopostback(“+((linkbutton)e.item.cells[4].controls[0]).clientid.replace(“__”,”$_”)+”,)”);
            }
            if(e.item.itemtype==listitemtype.edititem)
            {
                e.item.attributes.add(“ondblclick”,”__dopostback(“+((linkbutton)e.item.cells[5].controls[0]).clientid.replace(“__”,”$_”)+”,)”);
                for(int i=0;i<4;i++)((textbox)e.item.cells[i].controls[0]).attributes.add(“onmouseover”,”this.select()”);
            }
        }

        private void datagrid1_itemcommand(object source, system.web.ui.webcontrols.datagridcommandeventargs e)
        {
            if(e.commandname==”edit”)
            {
                this.datagrid1.edititemindex=e.item.itemindex;
                setbind();
            }
            if(e.commandname==”update”)
            {
                try
                {
                    sqlconnection conn=new sqlconnection(system.configuration.configurationsettings.appsettings[“strconn”]);
                    sqlcommand comm=new sqlcommand(“update employees set firstname=@firstname,lastname=@lastname,title=@title,birthdate=@birthdate where employeeid=@employeeid”,conn);
                    sqlparameter parm1=new sqlparameter(“@firstname”,sqldbtype.nvarchar,20);
                    parm1.value=((textbox)e.item.cells[0].controls[0]).text;
                    sqlparameter parm2=new sqlparameter(“@lastname”,sqldbtype.nvarchar,10);
                    parm2.value=((textbox)e.item.cells[1].controls[0]).text;
                    sqlparameter parm3=new sqlparameter(“@title”,sqldbtype.nvarchar,20);
                    parm3.value=((textbox)e.item.cells[2].controls[0]).text;
                    sqlparameter parm4=new sqlparameter(“@birthdate”,sqldbtype.datetime);
                    parm4.value=((textbox)e.item.cells[3].controls[0]).text;
                    sqlparameter parm5=new sqlparameter(“@employeeid”,sqldbtype.int);
                    parm5.value=this.datagrid1.datakeys[e.item.itemindex];
                    comm.parameters.add(parm1);
                    comm.parameters.add(parm2);
                    comm.parameters.add(parm3);
                    comm.parameters.add(parm4);
                    comm.parameters.add(parm5);
                    conn.open();
                    comm.executenonquery();
                    conn.close();
                }
                catch
                {
                    response.write(“<script>alert(输入的数据格式有误);</script>”);
                }
                finally
                {
                    this.datagrid1.edititemindex=-1;
                    setbind();
                }
            }
        }

    }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用单击和双击更新DataGrid中的数据的例子-.NET教程,数据库应用
分享到: 更多 (0)