欢迎光临
我们一直在努力

asp.net datagrid实现多层表头-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

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;
using system.xml;
using system.text;

namespace winneroa.newrpt
{
 /// <summary>
 /// dayenter 的摘要说明。
 /// </summary>
 public class dayenter : system.web.ui.page
 {
  protected system.web.ui.webcontrols.calendar calendar1;
  protected system.web.ui.webcontrols.button updatebutton;
  protected system.web.ui.webcontrols.datagrid datatable;
  protected system.web.ui.webcontrols.button addlinebutton;
 
  private void page_load(object sender, system.eventargs e)
  {
   if(this.initreaddatapopedom())
   {
    if(!this.ispostback)
    {
     this.calendar1.selecteddate=datetime.today;
     
     
    }
    this.initdata();
   }
   else
   {
    this.response.clear();
    this.response.write(“您没有权限!”);
    this.response.end();
    return;
   }
   if(this.initwritedatapopedom())
   {
     
    this.addlinebutton.visible=true;
    this.updatebutton.visible=true;
   }
   else
   {
    this.addlinebutton.visible=false;
    this.updatebutton.visible=false;
   }
   
  }
  
  private bool initwritedatapopedom()
  {
   arraylist depts=new arraylist();
   xmldocument xmldocument=new xmldocument();
   xmlnodereader xmlreader=null;
   try
   {
    
    xmldocument.load(server.mappath(“..”).tostring()+@”\popedom.xml”);
    xmlreader=new xmlnodereader(xmldocument.childnodes[1].childnodes[11]);
    while(xmlreader.read())
    {
     if(xmlreader.nodetype.equals(xmlnodetype.element))
     {
      if(xmlreader.name.tostring().equals(“write”))
      {
       while(xmlreader.read())
       {
        if(!xmlreader.name.equals(“user”))
        {
         break;
        }
        else
        {
         depts.add (xmlreader.readstring());
        }
       }
      }
     
     }
    }
   }
   catch(exception err)
   {
    response.write(err.tostring());
   }
   finally
   {
    xmlreader.close();
   }
   string deptname=request.cookies[“oabusyusername”].value.tostring();
   encoding e=encoding.getencoding(“gb2312”);
   deptname=httputility.urldecode(deptname,e);
   if(depts.contains(deptname))
   {
    return true;
   }
   else
   {
    return false;
   }
  }
  private bool initreaddatapopedom()
  {
   arraylist depts=new arraylist();
   xmldocument xmldocument=new xmldocument();
   xmlnodereader xmlreader=null;
   try
   {
    
    xmldocument.load(server.mappath(“..”).tostring()+@”\popedom.xml”);
    xmlreader=new xmlnodereader(xmldocument.childnodes[1].childnodes[11]);
    while(xmlreader.read())
    {
     if(xmlreader.nodetype.equals(xmlnodetype.element))
     {
      if(xmlreader.name.tostring().equals(“read”))
      {
       while(xmlreader.read())
       {
        if(!xmlreader.name.equals(“user”))
        {
         break;
        }
        else
        {
         depts.add (xmlreader.readstring());
        }
       }
      }
     
     }
    }
   }
   catch(exception err)
   {
    response.write(err.tostring());
   }
   finally
   {
    xmlreader.close();
   }
   string deptname=request.cookies[“oabusyusername”].value.tostring();
   encoding e=encoding.getencoding(“gb2312”);
   deptname=httputility.urldecode(deptname,e);
   if(depts.contains(deptname))
   {
    return true;
   }
   else
   {
    return false;
   }
  }

  private void initdata()
  {
   if(session[“dataset”]==null)
   {
    string sqlcmd=”select  id , lanewayname as 巷道名称, constructunit as 施工单位,designlength as 设计长度, designdm as 设计断面, mplane as 本月计划, dplaneenter as 本日,dfactenter as 本日1, mtotal as 本月累计, lithology as 岩性, angle as 角度,       mplaneenter as 本月1, mfactenter as 本月2, hair as 成巷1, moulding as 成巷2, memo as 备注 from dayenter where [date]=”+this.calendar1.selecteddate.toshortdatestring()+””;
    string connstring=”workstation id=server;packet size=4096;user id=sa;data source=.;persist security info=true;initial catalog=winneroa;password=pxh2003″;
    sqlconnection conn=new sqlconnection(connstring);
    try
    {
     conn.open();
    }
    catch(exception err)
    {
     response.write(err.tostring());
     return;
    }
    sqldataadapter sda=new sqldataadapter(sqlcmd,conn);
    dataset ds=new dataset();
  
    try
    {
     sda.fill(ds);
     
     this.datatable.datasource=ds.tables[0].defaultview;
   
     this.datatable.databind();
     this.session[“dataset”]=ds;
    }
    catch(exception err)
    {
     response.write(err.tostring());

    }
    finally
    {
    
     conn.close();
    }
   }
   else
   {
    dataset ds=(dataset)(session[“dataset”]);
    
    //datarow newrow=ds.tables[0].newrow();
    //ds.tables[0].rows.add(newrow);
    this.datatable.datasource=ds;
    this.datatable.databind();
    session[“dataset”]=ds;
   }
   
   
  }
  private void addline()
  {
   if(session[“dataset”]!=null)
   {
    dataset ds=(dataset)(session[“dataset”]);
    
    datarow newrow=ds.tables[0].newrow();
    ds.tables[0].rows.add(newrow);
    this.datatable.datasource=ds;
    this.datatable.databind();
    session[“dataset”]=ds;
   }
  }

  private void deldata()
  {
   string sqlcmd=”delete from dayenter where [date]=”+this.calendar1.selecteddate.toshortdatestring()+””;
   string connstring=”workstation id=server;packet size=4096;user id=sa;data source=.;persist security info=true;initial catalog=winneroa;password=pxh2003″;
   sqlconnection conn=new sqlconnection(connstring);
   try
   {
    conn.open();
   }
   catch(exception err)
   {
    response.write(err.tostring());
    return;
   }
   sqlcommand cmd=new sqlcommand(sqlcmd,conn);
   try
   {
    cmd.executenonquery();
   }
   catch(exception err)
   {
    response.write(err.tostring());
   }
   finally
   {
    conn.close();
   }
  }

  private void savedata()
  {
   this.deldata();
   string connstring=”workstation id=server;packet size=4096;user id=sa;data source=.;persist security info=true;initial catalog=winneroa;password=pxh2003″;
   sqlconnection conn=new sqlconnection(connstring);
   foreach(datagriditem e in this.datatable.items)
   {
    if(e.itemtype!=listitemtype.header && e.itemtype!=listitemtype.footer)
    {
     string sqlcmd=”insert into dayenter(lanewayname,constructunit,designlength,designdm,mplane,dplaneenter,dfactenter,mtotal,lithology,angle,mplaneenter,mfactenter,hair,moulding,memo,[date]) values(“;
     
     
     for(int j=2;j<e.cells.count ;j++)
     {
      sqlcmd+=””+((textbox)e.cells[j].controls[0]).text +”,”;
      
     }
     sqlcmd+=””+this.calendar1.selecteddate.toshortdatestring()+”)”;
     try
     {
      conn.open();
     }
     catch(exception err)
     {
      response.write(err.tostring());
      return;
     }
     sqlcommand cmd=new sqlcommand(sqlcmd,conn);
     try
     {
      cmd.executenonquery();
      
     }
     catch(exception err)
     {
      response.write(err.tostring()+sqlcmd);
     }
     finally
     {
      conn.close();
     }
    }
   }
   this.session[“dataset”]=null;
   this.initdata();
  }
  #region web 窗体设计器生成的代码
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 该调用是 asp.net web 窗体设计器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 – 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void initializecomponent()
  {   
   this.addlinebutton.click += new system.eventhandler(this.addlinebutton_click);
   this.updatebutton.click += new system.eventhandler(this.updatebutton_click);
   this.calendar1.selectionchanged += new system.eventhandler(this.calendar1_selectionchanged);
   this.datatable.itemcreated += new system.web.ui.webcontrols.datagriditemeventhandler(this.datatable_itemcreated);
   this.datatable.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.datatable_itemdatabound);
   this.load += new system.eventhandler(this.page_load);

  }
  #endregion

  private void addlinebutton_click(object sender, system.eventargs e)
  {
   this.savedata();
   this.addline();
   
  }

  private void updatebutton_click(object sender, system.eventargs e)
  {
   this.savedata();
  }

  private void datatable_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
  {
   
   
   e.item.cells[1].visible=false;
   if(e.item.itemtype!=listitemtype.header && e.item.itemtype!=listitemtype.footer)
   {
    button delbutton=new button();
    delbutton.text=”删除”;
    delbutton.id=e.item.cells[1].text+”d”;
    delbutton.click+=new eventhandler(delbutton_click);
    e.item.cells[0].controls.add(delbutton);
    for(int i=2;i<e.item.cells.count;i++)
    {
     textbox textbox=new textbox();
     if(!e.item.cells[i].text.equals(“&nbsp;”))
      textbox.text=e.item.cells[i].text;
     textbox.width=new unit(“88px”);
     
     if(!this.initwritedatapopedom())
     {
      textbox.readonly=true;
      e.item.cells[0].visible=false;
     }
     e.item.cells[i].controls.add(textbox);
    }
   }
  }

  private void datatable_itemcreated(object sender, system.web.ui.webcontrols.datagriditemeventargs e)
  {
   
   if (e.item.itemtype == listitemtype.header)
   {

    e.item.visible=false;
    table tb = (table)datatable.controls[0];
    datagriditem dgi1=new datagriditem(0,0,listitemtype.header);
    datagriditem dgi2 = new  datagriditem(0,0,listitemtype.header);
    tablecell[] cell1=new tablecell[14];
    for(int i=0;i<cell1.length;i++)
    {
     cell1[i]=new tablecell();
     if(i==7 || i==11|| i==12)
     {
      cell1[i].columnspan=2;
      cell1[i].horizontalalign=horizontalalign.center;
     }
     else
     {
      cell1[i].rowspan=2;
     }
    }
    cell1[0].text=”删除”;
    cell1[1].text=”编号”;
    cell1[1].visible=false;
    cell1[2].text=”巷号名称”;
    cell1[3].text=”施工单位”;
    cell1[4].text=”设计长度”;
    cell1[5].text=”设计断面”;
    cell1[6].text=”本月计划”;
    cell1[7].text=”本日”;
    cell1[8].text=”本月进度累计”;
    cell1[9].text=”岩性”;
    cell1[10].text=”角度”;
    cell1[11].text=”本月”;
    cell1[12].text=”成巷”;
    cell1[13].text=”备注”;
    
    for(int i=0;i<cell1.length;i++)
    {
     dgi1.cells.add(cell1[i]);
    }
    tablecell[] cell2=new tablecell[6];
    for(int i=0;i<cell2.length;i++)
    {
     cell2[i]=new tablecell();
     
    }
    cell2[0].text=”计划进度”;
    cell2[1].text=”实际进度”;
    cell2[2].text=”巷道累计”;
    cell2[3].text=”进尺长度”;
    cell2[4].text=”毛面”;
    cell2[5].text=”成型”;
    for(int i=0;i<cell2.length;i++)
    {
     dgi2.cells.add(cell2[i]);
     
    }

    tb.rows.addat(0,dgi1);
    tb.rows.addat(1,dgi2);
   }
   e.item.cells[1].visible=false;
  }

  private void calendar1_selectionchanged(object sender, system.eventargs e)
  {
   this.session[“dataset”]=null;
   this.initdata();
  }

  private void delbutton_click(object sender, eventargs e)
  {
   button tmpbutton=(button)sender;
   string id=tmpbutton.id;
   id=id.remove(id.length-1,1);
   string sqlcmd=”delete from dayenter where id=”+id;
   string connstring=”workstation id=server;packet size=4096;user id=sa;data source=.;persist security info=true;initial catalog=winneroa;password=pxh2003″;
   sqlconnection conn=new sqlconnection(connstring);
   try
   {
    conn.open();
   }
   catch(exception err)
   {
    response.write(err.tostring());
   }
   sqlcommand cmd=new sqlcommand(sqlcmd,conn);
   try
   {
    cmd.executenonquery();
    this.session[“dataset”]=null;
    this.initdata();
   }
   catch(exception err)
   {
    response.write(err.tostring()+sqlcmd);
   }
   finally
   {
    conn.close();
   }
  }
 }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » asp.net datagrid实现多层表头-.NET教程,Asp.Net开发
分享到: 更多 (0)