您的位置:首页 > 编程语言 > ASP

手工创建datagrid数据列/模板列/按钮事件+简单的数据操作类(asp.net)

2008-04-16 10:38 381 查看

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)




...{


// 在此处放置用户代码以初始化页面


}






Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码


override protected void OnInit(EventArgs e)




...{


//


// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。


//


InitializeComponent();


base.OnInit(e);


}






/**//// <summary>


/// 设计器支持所需的方法 - 不要使用代码编辑器修改


/// 此方法的内容。


/// </summary>


private void InitializeComponent()




...{


this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);


this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);


this.Button1.Click += new System.EventHandler(this.Button1_Click);


this.Load += new System.EventHandler(this.Page_Load);




}


#endregion




private void Button1_Click(object sender, System.EventArgs e)




...{


initData();


}


string strconn="server=;user id=sa;password=;database=clothing";


private void initData()




...{




string strSql="select * from tblProduct";


DataSet ds=new DataSet();


ds=SqlAccess.getDataSet(this.strconn,strSql);




this.DataGrid1.DataSource=ds.Tables[0].DefaultView;


DataGridCols.CreateCols(this.DataGrid1,"Pro_ID","叙号",50);


DataGridCols.CreateCols(this.DataGrid1,"Pro_Code","编号",50);


DataGridCols.CreateCols(this.DataGrid1,"Pro_Name","名称",100);


DataGridCols.CreateTemplateCol(this.DataGrid1,"Type","类型");


DataGridCols.CreateButton(this.DataGrid1,"del","删除");


this.DataGrid1.DataBind();


}




private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)




...{


string strSql="select * from tblProduct_Type";


DataSet ds=new DataSet();


ds=SqlAccess.getDataSet(this.strconn,strSql);


if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)




...{


DropDownList ddl=(DropDownList)e.Item.FindControl("Type");


ddl.DataSource=ds.Tables[0];


ddl.DataTextField="Type_Name";


ddl.DataValueField="Type_ID";


ddl.DataBind();


ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"Type_ID"))).Selected=true;


}


}




private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)




...{


if(e.CommandName=="del")




...{


SqlConnection conn=new SqlConnection(this.strconn);


SqlCommand comm=new SqlCommand("delete tblProduct where Pro_ID=@id",conn);


SqlParameter parm1=new SqlParameter("@id",SqlDbType.Int);


parm1.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];


comm.Parameters.Add(parm1);


conn.Open();


comm.ExecuteNonQuery();


conn.Close();


this.initData();


}




}


}


}





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐