国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

datagrid數據列/模板列/按鈕事件+操作類

2019-11-18 11:58:17
字體:
來源:轉載
供稿:網友

  1)創建datagrid數據列/模板列/按鈕的操作類:
  
  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 WeBTest
  {
  /// <summary>
  /// DataGridColumn 的摘要說明。
  /// </summary>
  public class DataGridCols
  {
  public void DataGridColumn()
  {
  //
  // TODO: 在此處添加構造函數邏輯
  //
  }
  
  public static void CreateCols(System.Web.UI.WebControls.DataGrid DataGrid1,string dataField,string headerText,int i)
  {
  BoundColumn cm=new BoundColumn();
  cm.DataField=dataField;
  cm.HeaderText=headerText;
  cm.HeaderStyle.Width=i;
  DataGrid1.Columns.Add(cm);
  }
  public static void CreateButton(System.Web.UI.WebControls.DataGrid DataGrid1,string commandName,string strText)
  {
  ButtonColumn bc=new ButtonColumn();
  bc.ButtonType=ButtonColumnType.PushButton;
  bc.CommandName=commandName;
  bc.HeaderText="操作";
  bc.Text=strText;
  DataGrid1.Columns.Add(bc);
  }
  
  public static void CreateTemplateCol(System.Web.UI.WebControls.DataGrid DataGrid1,string ID,string headerText)
  {
  TemplateColumn tm=new TemplateColumn();
  tm.ItemTemplate=new DDListCol(ID);
  tm.HeaderText=headerText;
  DataGrid1.Columns.Add(tm);
  }
  }
  }
  
  2)簡單的數據庫操作類
  
  using System;
  using System.Data;
  using System.Data.SqlClient;
  namespace Webtest
  {
  /// <summary>
  /// Sqlaccess 的摘要說明。
  /// </summary>
  public class SqlAccess
  {
  
  // string strConn="server=;user id=sa;passWord=;database=clothing";
  // DataSet ds;
  // SqlDataAdapter da;
  public SqlAccess()
  {
  //
  // TODO: 在此處添加構造函數邏輯
  //
  }
  public static void fillDataSet(string strConnection,string strSql,DataSet ds,string tableName)
  {
  if (strConnection==null strConnection.Length==0)
  {
  throw new ArgumentNullException( "strConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  if (ds==null)
  {
  throw new ArgumentNullException( "DataSet" );
  }
  if (tableName==null tableName.Length==0)
  {
  throw new ArgumentNullException( "tableName" );
  }
  using(SqlConnection conn=new SqlConnection(strConnection))
  {
  conn.Open();
  SqlDataAdapter da =new SqlDataAdapter(strSql,conn);
  da.Fill(ds,tableName);
  conn.Close();
  }
  }
  public static void fillDataSet(SqlConnection conn,string strSql,DataSet ds,string tableName)
  {
  if (conn==null)
  {
  throw new ArgumentNullException( "SqlConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  if (ds==null)
  {
  throw new ArgumentNullException( "DataSet" );
  }
  if (tableName==null tableName.Length==0)
  {
  throw new ArgumentNullException( "tableName" );
  }
  using(SqlDataAdapter da =new SqlDataAdapter(strSql,conn))
  {
  da.Fill(ds,tableName);
  conn.Close();
  }
  }
  
  public static DataSet getDataSet(string strConnection,string strSql)
  {
  if (strConnection==null strConnection.Length==0)
  {
  throw new ArgumentNullException( "strConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlConnection conn=new SqlConnection(strConnection))
  {
  DataSet ds=new DataSet();
  conn.Open();
  SqlDataAdapter da =new SqlDataAdapter(strSql,conn);
  da.Fill(ds);
  conn.Close();
  return ds;
  }
  }
  public static DataSet getDataSet(SqlConnection conn,string strSql)
  {
  if (conn==null)
  {
  throw new ArgumentNullException( "SqlConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlDataAdapter da =new SqlDataAdapter(strSql,conn))
  {
  DataSet ds=new DataSet();
  da.Fill(ds);
  conn.Close();
  return ds;
  }
  }
  public static int executeNonQuery(string strConnection,string strSql)
  {
  if (strConnection==null strConnection.Length==0)
  {
  throw new ArgumentNullException( "strConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlConnection conn=new SqlConnection(strConnection))
  {
  SqlCommand sqlCmd=new SqlCommand(strSql,conn);
  int i= sqlCmd.ExecuteNonQuery();
  conn.Close();
  return i;
  }
  }
  public static int executeNonQuery(SqlConnection conn,string strSql)
  {
  if (conn==null)
  {
  throw new ArgumentNullException( "SqlConnection" );
  }
  if (strSql==null strSql.Length==0)
  {
  throw new ArgumentNullException( "strSql" );
  }
  using(SqlCommand sqlCmd=new SqlCommand(strSql,conn))
  {
  int i=sqlCmd.ExecuteNonQuery();
  conn.Close();
  return i;
  }
  }
  }
  }
  
  3)創建模板列的類(可以創建n種模板列)
  
  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 Webtest
  {
  //DropDownList模板列
  public class DDListCol : ITemplate
  {
  string ID;
  public DDListCol(string id)
  {
  this.ID=id;
  }
  public void InstantiateIn(Control container)
  {
  DropDownList dpl = new DropDownList();
  dpl.ID=this.ID ;
  container.Controls.Add(dpl);
  
  }
  }
  //CheckBox模板列
  public class CheckBoxCol : ITemplate
  {
  string ID;
  public CheckBoxCol(string id)
  {
  this.ID=id;
  }
  public void InstantiateIn(Control container)
  {
  CheckBox checkbox = new CheckBox();
  checkbox.ID=this.ID ;
  container.Controls.Add(checkbox);
  }
  }
  }
  
  4)實例:創建數據源和創建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;
  using System.Data.SqlClient;
  namespace Webtest
  {
  /// <summary>
  /// WebForm1 的摘要說明。
  /// </summary>
  public class WebForm1 : System.Web.UI.Page
  {
  PRotected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Button Button1;
  
  private void Page_Load(object sender, System.EventArgs e)
  {
  // 在此處放置用戶代碼以初始化頁面
  }
  
  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平湖市| 兴和县| 军事| 洛阳市| 花莲市| 台前县| 哈巴河县| 永城市| 勃利县| 淮阳县| 永川市| 张家口市| 星子县| 云和县| 文安县| 长兴县| 嵩明县| 同仁县| 松阳县| 巴南区| 无锡市| 三台县| 芜湖县| 安阳县| 石泉县| 荥经县| 河津市| 恭城| 昌宁县| 新邵县| 商城县| 安达市| 皮山县| 泰安市| 郴州市| 仲巴县| 慈溪市| 翁牛特旗| 碌曲县| 道真| 陆河县|