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: 在此处添加构造函数逻辑 // }
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;