国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

手工創(chuàng)建datagrid數(shù)據(jù)列/模板列/按鈕事件+簡單的數(shù)據(jù)操作類(asp.net)

2024-07-10 12:55:23
字體:
供稿:網(wǎng)友

 

1)創(chuàng)建datagrid數(shù)據(jù)列/模板列/按鈕的操作類:

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: 在此處添加構(gòu)造函數(shù)邏輯
   //
  }

  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)簡單的數(shù)據(jù)庫操作類

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: 在此處添加構(gòu)造函數(shù)邏輯
   //
  }
  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)創(chuàng)建模板列的類(可以創(chuàng)建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)實(shí)例:創(chuàng)建數(shù)據(jù)源和創(chuàng)建datagrid數(shù)據(jù)列

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 窗體設(shè)計器生成的代碼
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 該調(diào)用是 asp.net web 窗體設(shè)計器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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();
   }

  }
 }
}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹阳市| 方城县| 泌阳县| 舟山市| 马边| 吉安县| 璧山县| 屯门区| 剑河县| 麟游县| 平舆县| 长阳| 合阳县| 永泰县| 剑川县| 和林格尔县| 蛟河市| 莱芜市| 连平县| 牡丹江市| 五原县| 梁平县| 德江县| 景泰县| 蛟河市| 大庆市| 安远县| 莱西市| 章丘市| 衡水市| 台南市| 贺州市| 汝阳县| 安吉县| 玉龙| 玉山县| 措美县| 土默特右旗| 黔西| 无极县| 永嘉县|