欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net的GridView控件使用方法大全

程序员文章站 2024-03-03 17:49:16
前台.aspx复制代码 代码如下:

前台.aspx

复制代码 代码如下:

<asp:label id="tplb" runat="server" text="总页数:"></asp:label>
<asp:label id="lblpagecount" runat="server" text=""></asp:label>
<asp:label id="curlabel" runat="server" text="当前页:"></asp:label>
<asp:label id="lblpage" text="1" runat="server"></asp:label>  
<asp:linkbutton id="lblfirstbutton" runat="server" onclick="lblfirstbutton_click" >|<</asp:linkbutton>  
<asp:linkbutton id="lblprebutton" runat="server" onclick="lblprebutton_click" ><</asp:linkbutton>  
<asp:linkbutton id="lblnextbutton" runat="server" onclick="lblnextbutton_click" >></asp:linkbutton> 
<asp:linkbutton id="lbllastbutton" runat="server" onclick="lbllastbutton_click" >>|</asp:linkbutton>  
<asp:dropdownlist id="ddlpage" runat="server" width="40px" autopostback="true" 
      onselectedindexchanged="ddlpage_selectedindexchanged"> 
      <asp:listitem>10</asp:listitem> 
        <asp:listitem>15</asp:listitem> 
      <asp:listitem>20</asp:listitem> 
      <asp:listitem>30</asp:listitem> 
</asp:dropdownlist> 
<asp:label id="pagesizelabel" runat="server" text="条/页"></asp:label>  

后台  
复制代码 代码如下:

#region分页
protected void bindfollowexaminfogridview(int personid)
  {
    int currentpage = convert.toint32(lblpage.text);
    datatable dt = new datatable();
    dt = feibf.getbypersonidfollowexaminfo(personid);  //查询指定人的随访信息记录
    if (dt.rows.count > 0)
    {
      followexaminfogridview.datasource = dt;
      followexaminfogridview.databind();
      pageddatasource ps = new pageddatasource();
      ps.datasource = dt.defaultview;
      ps.allowpaging = true;
      ps.pagesize = convert.toint32(ddlpage.selectedvalue);
      lblpagecount.text = ps.pagecount.tostring();
      this.lblprebutton.enabled = true;
      this.lblnextbutton.enabled = true;
      ps.currentpageindex = currentpage - 1;
      if (currentpage == 1)
      {
        this.lblprebutton.enabled = false;
        this.lblfirstbutton.enabled = false;
      }
      else
      {
        this.lblprebutton.enabled = true;
        this.lblfirstbutton.enabled = true;
      }
      if (currentpage == ps.pagecount)
      {
        this.lblnextbutton.enabled = false;
        this.lbllastbutton.enabled = false;
      }
      else
      {
        this.lblnextbutton.enabled = true;
        this.lbllastbutton.enabled = true;
      }
      followexaminfogridview.datasource = ps;
      followexaminfogridview.databind();
    }
    
  }
  protected void lblprebutton_click(object sender, eventargs e)
  {
    this.lblpage.text = convert.tostring(convert.touint32(lblpage.text) - 1);
    bindfollowexaminfogridview(convert.toint32(request.querystring["personid"]));
  }
  protected void lblnextbutton_click(object sender, eventargs e)
  {
    this.lblpage.text = convert.tostring(convert.touint32(lblpage.text) + 1);
    bindfollowexaminfogridview(convert.toint32(request.querystring["personid"]));
  }
  protected void lblfirstbutton_click(object sender, eventargs e)
  {
    this.lblpage.text = "1";
    bindfollowexaminfogridview(convert.toint32(request.querystring["personid"]));
  }
  protected void lbllastbutton_click(object sender, eventargs e)
  {
    this.lblpage.text = lblpagecount.text;
    bindfollowexaminfogridview(convert.toint32(request.querystring["personid"]));
  }
  protected void ddlpage_selectedindexchanged(object sender, eventargs e)
  {
    lblpage.text = "1";
    bindfollowexaminfogridview(convert.toint32(request.querystring["personid"]));
  }
#endregion 

排序
allowsort = "true"
sortexpression = "id"
dataview dv = sortbindgrid(dt);
#region排序
  protected void followexaminfogridview_sorting(object sender, gridviewsorteventargs e)
  {
    viewstate["sortexpression"] = e.sortexpression;
    if (viewstate["sortdirection"] == null)
    {
      viewstate["sortdirection"] = "asc";
    }
    else
    {
      if (viewstate["sortdirection"].tostring() == "asc")
      {
        viewstate["sortdirection"] = "desc";
      }
      else
      {
        viewstate["sortdirection"] = "asc";
      }
    }
   
    bindfollowexaminfogridview(convert.toint32(hiddenpersonid.value));
  }
  public dataview sortbindgrid(datatable table)
  {
    if (table != null)
    {
      dataview dv = table.defaultview;
      if (viewstate["sortexpression"] != null && viewstate["sortdirection"] != null)
      {
        dv.sort = viewstate["sortexpression"].tostring() + " " + viewstate["sortdirection"].tostring();
      }
      return dv;
    }
    else
    {
      return null;
    }
  }
  #endregion 

=======自带分页
  #region自带分页

protected void followexaminfogridview_pageindexchanging(object sender, gridviewpageeventargs e)
  {
    followexaminfogridview.pageindex = e.newpageindex;
    bindfollowexaminfogridview(convert.toint32(hiddenpersonid.value));
  }
#endregion


  选中grid view 的实现
复制代码 代码如下:

  #region实现选中行
   <selectedrowstyle backcolor="aliceblue" forecolor="gray" />
   <asp:commandfield showselectbutton="true"/>
if (e.row.rowtype == datacontrolrowtype.datarow)
  {
      e.row.attributes.add("onclick", "this.cells[0].childnodes[0].click()");
}
protected void gridviewregiment_selectedindexchanged(object sender, eventargs e)
{
    gridviewrow row = gridviewregiment.selectedrow;
    int regimentid = convert.toint32(row.cells[1].text);
    response.redirect("updateregimentation.aspx?regimentid=" + regimentid);
}
#endregion

显示颜色和删除
复制代码 代码如下:

  protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
  {
    //int i;
    //for (i = 0; i < gridviewregiment.rows.count; i++)
    //{
      if (e.row.rowtype == datacontrolrowtype.datarow)
      {
        //当有编辑列时,避免出错,要加的rowstate判断
        if (e.row.rowstate == datacontrolrowstate.normal || e.row.rowstate == datacontrolrowstate.alternate)
        {
          ((imagebutton)e.row.cells[2].findcontrol("ibtndelete")).attributes.add("onclick", "javascript:return confirm('你确认要删除:"" + e.row.cells[0].text + ""吗?')");
        }
        e.row.attributes.add("onmouseover", "this.style.backgroundcolor='#00a9ff'");
        e.row.attributes.add("onmouseout", "this.style.backgroundcolor='#e6f5fa'");
      }
    //}


gridview空的处理
  1 显示无表头的空纪录,emptydatatext="没有记录"
  2 显示表头的空纪录
复制代码 代码如下:

datatable dt = new datatable();
  dt = feibf.getbypersonidfollowexaminfo(personid);  //查询指定人的随访信息记录
    dataview dv = sortbindgrid(dt);
    if (dt.rows.count > 0)
    {
      followexaminfogridview.datasource = dv;
      followexaminfogridview.databind();
    }
    else
    {
      //添加新行显示表头
      dt.rows.add(dt.newrow());
      followexaminfogridview.datasource = dt;
      followexaminfogridview.databind();
      //处理新行
      int columncount = followexaminfogridview.rows[0].cells.count;
      //清除掉该空行的全部单元格
      followexaminfogridview.rows[0].cells.clear();
      //新建单元格对象
      followexaminfogridview.rows[0].cells.add(new tablecell());
      //合并单元格
      followexaminfogridview.rows[0].cells[0].columnspan = columncount;
      //设置单元格提示内容
      followexaminfogridview.rows[0].cells[0].style.value = "text-align:center";
      followexaminfogridview.rows[0].cells[0].text = "此人无随访信息";
    } 

gridview 的导出
enableeventvalidation="false"
复制代码 代码如下:

#region导出
 public override void verifyrenderinginserverform(control control)
  {
  }
  protected void btnprint_click(object sender, eventargs e)
  {
    response.clear();
    response.buffer = true;
    response.charset = "gb2312";
    response.appendheader("content-disposition", "attachment;filename=filename.xls");
    // 如果设置为getencoding("gb2312");导出的文件将会出现乱码!!!
    response.contentencoding = system.text.encoding.utf7;
    response.contenttype = "application/ms-excel";//设置输出文件类型为excel文件。
    system.io.stringwriter ostringwriter = new system.io.stringwriter();
    system.web.ui.htmltextwriter ohtmltextwriter = new system.web.ui.htmltextwriter(ostringwriter);
    this.affichegv.rendercontrol(ohtmltextwriter);
    response.output.write(ostringwriter.tostring());
    response.flush();
    response.end();
  }
#endregion 

  tooltip gridview详细信息的显示
  前台
<script type="text/javascript" >
  function tooltip(cella,cellb)
  {
    document.getelementbyid("dc").innertext = "详细信息:"+cellb;
    document.getelementbyid("id").innertext = "id:"+cella;
    x= event.clientx+document.body.scrollleft;
    y=event.clienty+document.body.scrolltop+20;
    tooltiplayer.style.display="inline";
    tooltiplayer.style.left=x;
    tooltiplayer.style.top=y;
  }
</script>
<div id="tooltiplayer" style=" position:absolute; display:none;
  background-color:aqua; border-color:blue; border-style:solid;
   border-color:blue; border-width:1px; " >
  <table>
  <tr><td>affiche</td></tr>
  <tr><td id ="dc"></td></tr>
  <tr><td id ="id"> </td></tr>
  </table>
</div> 


后台
复制代码 代码如下:

protected void affichegv_rowdatabound(object sender, gridviewroweventargs e)
  {
    if (e.row.rowtype == datacontrolrowtype.datarow)
    {
      if (e.row.rowstate == datacontrolrowstate.normal || e.row.rowstate == datacontrolrowstate.alternate)
      {
1 e.row.attributes.add("onmouseover", "tooltip('" +e.row.cells[0].text.tostring()+ "','"+e.row.cells[1].text.tostring()+"')");
2 e.row.attributes.add("onmouseover","javascript:tooltip('e.row.cells[0].text');");
3 e.row.attributes.add("onmouseover", "tooltip('e.row.cells[0].text')");
      } }

#region自带编辑
  protected void gvaffiche_rowediting(object sender, gridviewediteventargs e)
  {
    gvaffiche.editindex = e.neweditindex;
    bindgvaffiche();
  }
  protected void gvaffiche_rowdeleting(object sender, gridviewdeleteeventargs e)
  {
    gvaffiche.editindex = -1;
    myaffiche.delafficebf( convert.toint32(gvaffiche.datakeys[e.rowindex].value.tostring()));
    bindgvaffiche();
  }
  protected void gvaffiche_rowupdating(object sender, gridviewupdateeventargs e)
  {
    int id = convert.toint32(((textbox)(gvaffiche.rows[e.rowindex].cells[0].controls[0])).text.tostring().trim());
    string dc = ((textbox)(gvaffiche.rows[e.rowindex].cells[1].controls[0])).text.tostring().trim();
    myaffiche.updateaffichebf(id,dc);
    gvaffiche.editindex = -1;
    bindgvaffiche();
  }
  protected void gvaffiche_rowcancelingedit(object sender, gridviewcancelediteventargs e)
  {
    gvaffiche.editindex = -1;
    bindgvaffiche();
  }
#endregion 

#region样式的控制
  protected void gvaffiche_rowdatabound(object sender, gridviewroweventargs e)
  {
    //首先判断是否是数据行
    if (e.row.rowtype == datacontrolrowtype.datarow)
    {
      //当有编辑列时,避免出错,要加的rowstate判断
      if (e.row.rowstate == datacontrolrowstate.normal || e.row.rowstate == datacontrolrowstate.alternate)
      {
        ((button)e.row.cells[7].findcontrol("btndel")).attributes.add("onclick","javascript:return confirm('你确认删除:"" + e.row.cells[1].text + ""')");
        //当鼠标停留时更改背景色
        e.row.attributes.add("onmouseover", "color=this.style.backgroundcolor;this.style.backgroundcolor='#00a9ff'");
        //当鼠标移开时还原背景色
        e.row.attributes.add("onmouseout","this.style.backgroundcolor=color");
        gvaffiche.attributes.add("style", "word-break:keep-all;word-wrap:normal");
        //gvaffiche.attributes.add("style", "word-break:break-all;word-wrap:break-word");
        if (e.row.cells[1].text == "444")
        {
          e.row.cells[1].backcolor = system.drawing.color.red;
        }
      }
    }
  }
  #endregion 


以上是gridview控件的一些基础使用大全,希望对大家有所用处。