簡體   English   中英

如何通過自定義打印窗口打印整個gridview區域?

[英]How can I print a full gridview area via a custom print window?

我正在創建一個自定義GridView數據“打印”窗口,該窗口旨在僅打印出gridview區域。 一個按鈕將打印出當前窗口的網格視圖,另一個按鈕將打印出GridView中的所有記錄。 就目前的代碼而言,單擊按鈕(分別打印當前頁面和所有記錄)時,預覽窗口將完美顯示GridView中的所有記錄(23列,包括GridView兩側的兩個命令字段)。 但是,當打印出作業時,無論我調整哪種打印機或CSS /格式設置,網格視圖都會被切碎(僅顯示大約一半的網格)。

我遇到的兩個挑戰是:1)我無法以縱向或橫向打印完整的gridview,並且2)我的JavaScript很弱。 我如何調整以下代碼,以便在打印gridview時打印出FULL網格?

如果需要更多信息,請不要猶豫。

這是我的aspx文件后面用於打印按鈕的代碼。

protected void PrintCurrentPage_Click(object sender, EventArgs e)
{
GridView1.PagerSettings.Visible = false;
GridView1.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
string gridHTML = sw.ToString().Replace("\"", "'")
.Replace(System.Environment.NewLine, "");
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload = new function(){");
sb.Append("var printWin = window.open('', '', 'left=0");
sb.Append(",top=0,width=3000,height=600,status=0');");
sb.Append("printWin.document.write(\"");
sb.Append(gridHTML);
sb.Append("\");");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();};");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
GridView1.PagerSettings.Visible = true;
GridView1.DataBind();

}

這是打印所有記錄的代碼。

protected void PrintAll_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
string gridHTML = sw.ToString().Replace("\"", "'")
    .Replace(System.Environment.NewLine, "");
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload = new function(){");
sb.Append("var printWin = window.open('', '', 'left=0");
sb.Append(",top=0,width=3000,height=600,status=0');");
sb.Append("printWin.document.write(\"");
sb.Append(gridHTML);
sb.Append("\");");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();};");
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
GridView1.AllowPaging = true;
GridView1.DataBind();


}

如果網格的寬度那么大,則沒有任何內容,默認情況下,您可以將其全部打印在同一頁面上...

但是,這並不是一個壞消息,實際上,有幾篇文章可以幫助您在用戶打印時以不同的方式選擇和顯示相同的網格。

我認為最好的方法是在一列中顯示更多信息,將其分組為與用戶相關的其他內容,然后嘗試...

這只是一個示例,我不知道您要打印什么,但是,與其將客戶的所有字段都放在一行中,不如將它們分組為:

Customer                  | Sales Responsible        | ...
------------------------------------------------------------------
Bruno Alexandre           | Techie Joe               | ...
My Street not yours, 56   | 43 sales this month      | ...
DK-1400 København         | 450.000€ per sale (avg)  | ...

然后您可以使用一class table-print裝飾該表,其中:

<style>
@media screen
{
  table-print {display:none;}
}
@media print
{
  table-print {display:block;}
}
@media screen,print
{
  ...
}
</style>

之后,請閱讀如何處理分頁符:

打印大型HTML表格時如何處理分頁符

甚至還有Printliminator可以幫助用戶禁用他們不想打印的內容...

要將GridView的方向更改為RTL:

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute(HtmlTextWriterStyle.Direction, "rtl");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM