Asp.net GridView控件使用常用范例

发布时间:2019-09-12编辑:脚本学堂
为大家介绍GridView控件应用的一些例子,包括基本应用、排序、自带分页、显示颜色与删除颜色、Gridview的导出等内容,有兴趣的朋友,可以参考下。


8、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

9、ToolTip GridView详细信息的显示
1)、前台代码:
 

复制代码 代码示例:
<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>

2)、后台代码:
 

复制代码 代码示例:
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)
      {
 e.Row.Attributes.Add("onmouseover", "Tooltip('" +e.Row.Cells[0].Text.ToString()+ "','"+e.Row.Cells[1].Text.ToString()+"')");
 e.Row.Attributes.Add("onmouseover","javascript:Tooltip('e.Row.Cells[0].Text');");
 e.Row.Attributes.Add("onmouseover", "Tooltip('e.Row.Cells[0].Text')");
      } }
}

10、#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

11、#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