服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

datagrid数据列/模板列/按钮事件+操作类


  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)