logo

一段选择打印列的代码

作者:孟宪会 阅读:752 发表于:2011-09-09 15:12:47

拷贝粘贴即可运行看效果

ASPX 代码
<%@ Page Language="C#" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<script runat="server">
  
// 计算数据,完全可以从数据库取得
  System.Data.DataTable CreateDataSourceByXianhuiMeng()
  {
    System.Data.DataTable dt
= new System.Data.DataTable();
    System.Data.DataRow dr;
    dt.Columns.Add(
new System.Data.DataColumn("学生班级", typeof(System.String)));
    dt.Columns.Add(
new System.Data.DataColumn("学生姓名", typeof(System.String)));
    dt.Columns.Add(
new System.Data.DataColumn("语文", typeof(System.Decimal)));
    dt.Columns.Add(
new System.Data.DataColumn("数学", typeof(System.Decimal)));
    dt.Columns.Add(
new System.Data.DataColumn("英语", typeof(System.Decimal)));
    dt.Columns.Add(
new System.Data.DataColumn("计算机", typeof(System.Decimal)));

    
for (int i = 0; i < 8; i++)
    {
      System.Random rd
= new System.Random(Environment.TickCount * i); ;
      dr
= dt.NewRow();
      dr[
0] = "孟子章" + i.ToString();
      dr[
1] = "xxx" + i.ToString();
      dr[
2] = System.Math.Round(rd.NextDouble() * 100, 2);
      dr[
3] = System.Math.Round(rd.NextDouble() * 100, 2);
      dr[
4] = System.Math.Round(rd.NextDouble() * 100, 2);
      dr[
5] = System.Math.Round(rd.NextDouble() * 100, 2);
      dt.Rows.Add(dr);
    }
    
return dt;
  }

  protected
void Page_Load(object sender, EventArgs e)
  {
    GridView1.DataSource
= this.CreateDataSourceByXianhuiMeng();
    GridView1.DataBind();    
  }

  protected
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    
if (e.Row.RowType == DataControlRowType.Header)
    {
      
for (int i = 0; i < e.Row.Cells.Count; i++)
      {
        e.Row.Cells[i].Attributes.Add(
"onclick", "PrintMe(this," + i.ToString() + ")");
        e.Row.Cells[i].Attributes.Add(
"class", "noprint");
      }
    }
    
if (e.Row.RowType == DataControlRowType.DataRow)
    {
      
for (int i = 0; i < e.Row.Cells.Count; i++)
      {
        e.Row.Cells[i].Attributes.Add(
"class", "noprint");
      }
    }
    
if (e.Row.RowType == DataControlRowType.Footer)
    {
      
for (int i = 0; i < e.Row.Cells.Count; i++)
      {
        e.Row.Cells[i].Attributes.Add(
"class", "noprint");
      }
    }
  }
</script>
<html>
<head>
<title></title>
  
<style type="text/css" media="print">
    .noprint
    
{
      display
: none;
      background
: #fff;
    
}
    .print
    
{
      background
: #fff;
    
}
  
</style>
  
<style type="text/css" media="screen">
    .print
    
{
      background
: red;
    
}
    .noprint
    
{
      background
: #fff;
    
}
  
</style>
  
<script type="text/javascript">
    
function PrintMe(cell, index) {
      
if (cell.getAttribute("printme") == "1") {
        cell.setAttribute(
"printme", "");
        
for (i = 0; i < document.getElementById("GridView1").rows.length; i++) {
          document.getElementById(
"GridView1").rows[i].cells[index].className = "noprint";
        }
      }
      
else {
        cell.setAttribute(
"printme", "1");
        
for (i = 0; i < document.getElementById("GridView1").rows.length; i++) {
          document.getElementById(
"GridView1").rows[i].cells[index].className = "print";
        }
      }
    }
  
</script>
</head>
<body>
<h2 class="noprint">点击列头选择打印的列</h2>
  
<form id="form1" runat="server">
  
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
    Font-Size
="12px" CellPadding="3">
    
<HeaderStyle BackColor="#EDEDED" />
    
<Columns>
      
<asp:TemplateField HeaderText="模版列">
        
<ItemTemplate>
          
<%#Eval("学生姓名") %>
        
</ItemTemplate>
      
</asp:TemplateField>
    
</Columns>
  
</asp:GridView>
  
</form>
</body>
</html>