You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET GridView打印第二页表头边框丢失问题求助

Fix Missing Border on GridView Header in Second Page When Printing

Hey there, I see you're dealing with a frustrating issue where the GridView header loses its borders on the second page when printing, even after adding page breaks. Let's break down why this is happening and fix it step by step.

Why This Happens

You've set HeaderStyle-BorderColor="Black", but when printing, browsers sometimes don't persist border styles for repeated headers—especially when using page-break-after. Also, your current code only sets the header's TableSection property when a page break row is hit, which means it might not be applied consistently across all pages.

Solution 1: Add Print-Specific CSS

The first fix is to use a print media query to force consistent styling for the table and headers across all printed pages. Add this to your <head> section:

<head runat="server">
<title></title>
<link href="../Content/Styles.css" rel="stylesheet" type="text/css" />
<style type="text/css">
    @media print {
        /* Ensure the header repeats on every page */
        thead {
            display: table-header-group;
        }
        /* Force border collapse to prevent missing borders */
        table {
            border-collapse: collapse !important;
        }
        /* Hardcode border styles for header cells */
        th {
            border: 1px solid black !important;
        }
        /* Match data cell borders to the header */
        td {
            border: 1px solid black !important;
        }
        /* Clean up page break styling */
        tr.page-break {
            page-break-after: always;
        }
    }
</style>
</head>

Solution 2: Fix the Code-Behind Logic

Your current RowDataBound method only configures the header when a page break row is processed. Instead, set up the header once when the page loads to ensure it's recognized as a persistent table header:

private int counter = 0; // Declare this as a class-level variable to track rows across bindings

protected void Page_Load(object sender, EventArgs e) {
    DSD.SelectParameters["mcod"].DefaultValue = Request["MCOD"];
    DSD.DataBind();
    
    // Set up the header for repeated printing once the GridView has data
    if (GridView1.Rows.Count > 0) {
        GridView1.UseAccessibleHeader = true;
        GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.DataRow) {
        counter++;
        if (counter % 10 == 0) {
            // Only add the page break style here—no need to reconfigure the header
            e.Row.Attributes.Add("style", "page-break-after: always;");
        }
    }
}

Quick Notes to Avoid Pitfalls

  • Don't forget the class-level counter: If you declare counter inside RowDataBound, it'll reset to 0 every time the method runs, and your page breaks won't work correctly.
  • Test in print preview: Different browsers render print styles differently—Chrome and Edge tend to handle repeated table headers the most reliably.
  • Remove redundant header config: You don't need to set UseAccessibleHeader and TableSection in RowDataBound anymore; doing it once in Page_Load is enough.

After making these changes, your GridView headers should show up with full borders on every printed page, and the page breaks will work as expected.

内容的提问来源于stack exchange,提问作者Андрей Голубцов

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:38:52