日期:2014-03-30  浏览次数:20452 次

 

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)