WEB开发网
开发学院WEB开发Jsp datagrid数据列/模板列/按钮事件+操作类 阅读

datagrid数据列/模板列/按钮事件+操作类

 2008-01-05 17:58:59 来源:WEB开发网   
核心提示:1)创建datagrid数据列/模板列/按钮的操作类:using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Data.SqlClient;using System.Drawing;

  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)

Tags:datagrid 数据 模板

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接