DataGrid综合(无模板列)

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;

namespace ooi_cn
{
 /// <summary>
 /// Board 的摘要说明。
 /// </summary>
 public class Board : System.Web.UI.Page
 {
  protected System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
  protected System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
  protected System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
  protected System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
  protected System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
  protected System.Data.OleDb.OleDbConnection oleDbConnection1;
  protected ooi_cn.DataSet2 dataSet21;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Data.OleDb.OleDbCommand oleDbCommand1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
oleDbDataAdapter1.Fill(dataSet21);
   DataGrid1.DataBind();
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter();
   this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand();
   this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection();
   this.dataSet21 = new ooi_cn.DataSet2();
   this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand();
   ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit();
   //
   // oleDbDataAdapter1
   //
   this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1;
   this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1;
   this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1;
   this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
                          new System.Data.Common.DataTableMapping("Table", "Socut_BBS", new System.Data.Common.DataColumnMapping[] {
                                                      new System.Data.Common.DataColumnMapping("ID", "ID"),
                                                      new System.Data.Common.DataColumnMapping("Content", "Content"),
                                                      new System.Data.Common.DataColumnMapping("Poster", "Poster")})});
   this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1;
   //
   // oleDbSelectCommand1
   //
   this.oleDbSelectCommand1.CommandText = "SELECT ID, Content, Poster FROM Socut_BBS ORDER BY ID DESC";
   this.oleDbSelectCommand1.Connection = this.oleDbConnection1;
   //
   // oleDbInsertCommand1
   //
   this.oleDbInsertCommand1.CommandText = "INSERT INTO Socut_BBS(Content, Poster) VALUES (?, ?)";
   this.oleDbInsertCommand1.Connection = this.oleDbConnection1;
   this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Content", System.Data.OleDb.OleDbType.VarWChar, 0, "Content"));
   this.oleDbInsertCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Poster", System.Data.OleDb.OleDbType.VarWChar, 50, "Poster"));
   //
   // oleDbUpdateCommand1
   //
   this.oleDbUpdateCommand1.CommandText = "UPDATE Socut_BBS SET Content = ?, Poster = ? WHERE (ID = ?) AND (Poster = ? OR ? " +
    "IS NULL AND Poster IS NULL)";
   this.oleDbUpdateCommand1.Connection = this.oleDbConnection1;
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Content", System.Data.OleDb.OleDbType.VarWChar, 0, "Content"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Poster", System.Data.OleDb.OleDbType.VarWChar, 50, "Poster"));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ID", System.Data.DataRowVersion.Original, null));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Poster", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Poster", System.Data.DataRowVersion.Original, null));
   this.oleDbUpdateCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Poster1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Poster", System.Data.DataRowVersion.Original, null));
   //
   // oleDbDeleteCommand1
   //
   this.oleDbDeleteCommand1.CommandText = "DELETE FROM Socut_BBS WHERE (ID = ?) AND (Poster = ? OR ? IS NULL AND Poster IS N" +
    "ULL)";
   this.oleDbDeleteCommand1.Connection = this.oleDbConnection1;
   this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ID", System.Data.OleDb.OleDbType.Integer, 0, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ID", System.Data.DataRowVersion.Original, null));
   this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Poster", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Poster", System.Data.DataRowVersion.Original, null));
   this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_Poster1", System.Data.OleDb.OleDbType.VarWChar, 50, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Poster", System.Data.DataRowVersion.Original, null));
   //
   // oleDbConnection1
   //
   this.oleDbConnection1.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=""F:/1web/ooi_cn/#ooicn.mdb"";Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";
   //
   // dataSet21
   //
   this.dataSet21.DataSetName = "DataSet2";
   this.dataSet21.Locale = new System.Globalization.CultureInfo("zh-CN");
   //
   // oleDbCommand1
   //
   this.oleDbCommand1.Connection = this.oleDbConnection1;
   this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
   this.Load += new System.EventHandler(this.Page_Load);
   ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit();

  }
  #endregion

  private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex = e.Item.ItemIndex;
   DataGrid1.DataBind();

  }

  private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex = -1;
   DataGrid1.DataBind();

  }

  private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

   string Poster;
  
   TextBox tb;
   tb = (TextBox) e.Item.Cells[2].Controls[0];
   Poster = tb.Text;

   dsCategories.Socut_BBSRow r;
   r = dataSet21.Socut_BBS.FindByID(int.Parse(key));

   r.Poster = Poster;

   oleDbDataAdapter1.Update(dataSet21);
   DataGrid1.DataBind();

   DataGrid1.EditItemIndex = -1;

   DataGrid1.DataBind();
  }

  private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string delCmd="delete from SOCUT_BBS where ID="+DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString();
   oleDbCommand1.CommandText=delCmd;
   oleDbCommand1.Connection.Open();
   oleDbCommand1.ExecuteNonQuery();
   oleDbCommand1.Connection.Close();
   oleDbDataAdapter1.Fill(dataSet21);
   DataGrid1.EditItemIndex=-1;
   DataGrid1.DataBind();
  }
 }
}

阅读更多
想对作者说点什么? 我来说一句

DataGrid模板列自定义控件

2014年12月24日 60KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭