添加、删除更新、编辑全部代码(附件中带源程序)
											王杰小提示:一定要在datagrid的datakeyfiled中指定id,大小写不区分,否则会出错的!
此程序是datagrid控件动态添加、修改数据的代表,作为ado.net综合应用,将实现在不刷新页面的情况下的添加、修改数据,也就是它的优点!
下面是全部代码!
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace WebApplication12
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.Label Label5;
  protected System.Web.UI.WebControls.Label Label6;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.TextBox TextBox2;
  protected System.Web.UI.WebControls.TextBox TextBox3;
  protected System.Web.UI.WebControls.TextBox TextBox4;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  private void Page_Load(object sender, System.EventArgs e)
  {
   if (!IsPostBack) ReadRecords();
  }
  private void ReadRecords()
  {
   OleDbConnection conn=new OleDbConnection();
   conn.ConnectionString="provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("db1.mdb");
   string cmd="select * from table1";
   conn.Open();
   OleDbCommand comm=new OleDbCommand(cmd,conn);
   OleDbDataReader reader=comm.ExecuteReader();
   DataGrid1.DataSource=reader;
   DataGrid1.DataBind();
   conn.Close();
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {    
   this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
   this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
   this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
   this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
   this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
  private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex=-1;
   ReadRecords();
  }
  private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex=e.Item.ItemIndex;
   ReadRecords();
  }
  private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string sql="update table1 set 姓名='"+((TextBox)e.Item.Cells[2].Controls[0]).Text+"',语文="+((TextBox)e.Item.Cells[3].Controls[0]).Text+",数学="+((TextBox)e.Item.Cells[4].Controls[0]).Text+",英语="+((TextBox)e.Item.Cells[5].Controls[0]).Text+" where id="+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"";
   ExecuteNonQuery(sql);
   DataGrid1.EditItemIndex=-1;
   ReadRecords();
  }
  private void ExecuteNonQuery(string sql)
  {
   OleDbConnection conn=new OleDbConnection();
   try
   {
    conn.ConnectionString="provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("db1.mdb");
    conn.Open();
    OleDbCommand cmd=new OleDbCommand(sql,conn);
    cmd.ExecuteNonQuery();
   }
   catch (Exception e)
   {
    Response.Write(e.Message);
    Response.End();
   }
   finally
   {
    if (conn !=null) conn.Close();
   }
  }
  private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
  
  }
  private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   OleDbConnection conn=new OleDbConnection();
   conn.ConnectionString="provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("db1.mdb");
   string sql="delete from table1 where id="+DataGrid1.DataKeys[(int)e.Item.ItemIndex];
   OleDbCommand comm=new OleDbCommand(sql,conn);
   conn.Open();
   try
   {
    comm.ExecuteNonQuery();
    Label1.Text="<b>删除成功</b>";
   }
   catch (OleDbException)
   {
    Label1.Text="<b>删除失败!</b>";
    Label1.Style["color"]="red";
   }
   conn.Close();
   ReadRecords();
  }
  private void Button1_Click(object sender, System.EventArgs e)
  {
   OleDbConnection conn=new OleDbConnection();
   conn.ConnectionString="provider=microsoft.jet.oledb.4.0;data source="+Server.MapPath("db1.mdb");
   string sql="insert into table1(姓名,语文,数学,英语) values('"+TextBox1.Text+"',"+TextBox2.Text+","+TextBox3.Text+","+TextBox4.Text+")";
   OleDbCommand cmd=new OleDbCommand(sql,conn);
   conn.Open();
   try
   {
    cmd.ExecuteNonQuery();
    Label1.Text="添加成功!";
   }
   catch(OleDbException)
   {
    Label1.Text="添加失败!";
    Label1.Style["color"]="red";
   }
   conn.Close();
   ReadRecords();
  }
}
}
	
		
			
		
	
[此贴子已经被作者于2005-12-1 17:07:41编辑过]