使用单击和双击更新DataGrid中的数据的例子    

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

前台:

<%@ Page language="c#" Codebehind="WebForm87.aspx.cs" AutoEventWireup="false" Inherits="csdn.WebForm87" %>


   
        WebForm87
       
       
       
        http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
       
   
   
       
                            CellPadding="5" CssClass="border" DataKeyField="EmployeeID">
               
               
               
                   
                   
                   
                   
                   
                   
               

           

       


   


后台:


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


        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        ///

        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("");
                }
                finally
                {
                    this.DataGrid1.EditItemIndex=-1;
                    SetBind();
                }
            }
        }

    }
}


作者Blog:http://blog.csdn.net/lingfeng0626/

关联文档