62,243
社区成员




<script type="text/javascript">
function PrintGridView() {
// 打开一个新网页
var newwindow = window.open('printer','','');
// 将指定DIV中的内容写入该网页
newwindow.document.write(document.getElementById('dvBody').innerHTML);
newwindow.document.close();
// 打印该网页
newwindow.focus();
newwindow.print();
// 打印完毕后关闭该窗口
newwindow.close();
return;
}
</script>
<%@ Page Language="C#" %>
<!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] = "xxx" + 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>