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)
{
//
// 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 [email protected]",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();
}
}
}
}
新闻热点
疑难解答
图片精选