You must Sign In to post a response.
  • Category: ASP.NET

    Gridview fixed header not printing all the rows

    I have a grid view with fixed header when i try printing it prints only one page
    and not all the pages.
    more over i am printing using asp panel
    with labels and other texts also should be displayed.

    any further information will be provided.

    Thanks
  • #762885
    Hello Syed Thahir Hussain,

    Can you explain your query in detail so that i can understand, what exactly you want to do.

    In the explanation you had given i am not getting anything.

    Can you show the snapshot also if possible with explanation.?

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762888
    I can able to print but not all the rows.
    i need to be print in panel with labels as i have text to display with gridview like date etc

    about_blank.pdf

    Delete Attachment

  • #762889
    Hello Syed Thahir Hussain,

    As per my understanding you want to print the page.

    Refer the below code :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    <script type = "text/javascript">
    function Print() {
    var panel = document.getElementById("<%=Panel1.ClientID %>");
    var printWindow = window.open('', '', 'height=400,width=800');
    printWindow.document.write(panel.innerHTML);
    printWindow.document.close();
    setTimeout(function () {
    printWindow.print();
    }, 500);
    return false;
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Panel ID="Panel1" runat="server" ToolTip="I am Panel" BorderStyle="Solid"
    BorderWidth="2px">
    <asp:Label ID="Label1" runat="server"
    Text="I am Label" Font-Names="Segoe UI"></asp:Label>
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" CellPadding="4"
    Font-Names="Segoe UI" ForeColor="#333333">
    <RowStyle BackColor="#E3EAEB" />
    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#7C6F57" />
    <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <br />
    <br />
    </asp:Panel>
    <asp:Button ID="ButtonPrint" runat="server" Text="Print" OnClientClick = "return Print();" />
    </div>
    </form>
    </body>
    </html>


    Code - behind :

    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True;User Instance=True");
    SqlDataAdapter sda;

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    try
    {
    DataTable dt = new DataTable();
    con.Open();
    sda = new SqlDataAdapter("SELECT * FROM Employee",con);
    sda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    finally
    {
    con.Close();
    }
    }
    }

    See the attached image. It's an output i have got using above mentioned code.

    Hope this will help you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762890
    This code is working fine, but my requirement if page data contains more than 100 rows and i fix my header to view the header text.
    In this scenario i can't able to print all the 100 rows.
    able to print only records which i send the file.
    I need to print all the 100 rows with fixed header and scrollable and with Heading Text.

    Thanks

  • #762893
    Hello Syed Thahir Hussain,

    Refer the updated code :

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" CellPadding="4"
    Font-Names="Segoe UI" ForeColor="#333333">
    <RowStyle BackColor="#EFF3FB" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
    <br />
    <br />
    <asp:Button ID="ButtonPrint" runat="server" onclick="ButtonPrint_Click"
    Text="Print" />
    </div>
    </form>
    </body>
    </html>


    Code - behind :

    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Text;


    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True;User Instance=True");
    SqlDataAdapter sda;

    public void BindGrid()
    {
    try
    {
    DataTable dt = new DataTable();
    con.Open();
    sda = new SqlDataAdapter("SELECT EmpId AS 'Emp ID', EmpName AS 'Emp Name' FROM Employee", con);
    sda.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }
    catch (Exception ex)
    {
    Response.Write(ex.Message);
    }
    finally
    {
    con.Close();
    }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    BindGrid();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    /*Verifies that the control is rendered */
    }

    protected void ButtonPrint_Click(object sender, EventArgs e)
    {
    GridView1.UseAccessibleHeader = true;
    GridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
    GridView1.FooterRow.TableSection = TableRowSection.TableFooter;
    GridView1.Attributes["style"] = "border-collapse:separate";
    foreach (GridViewRow row in GridView1.Rows)
    {
    if (row.RowIndex % 10 == 0 && row.RowIndex != 0)
    {
    row.Attributes["style"] = "page-break-after:always;";
    }
    }
    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=1000,height=600,status=0');");
    sb.Append("printWin.document.write(\"");
    string style = "<style type = 'text/css'>thead {display:table-header-group;} tfoot{display:table-footer-group;}</style>";
    sb.Append(style + gridHTML);
    sb.Append("\");");
    sb.Append("printWin.document.close();");
    sb.Append("printWin.focus();");
    sb.Append("printWin.print();");
    sb.Append("printWin.close();");
    sb.Append("};");
    sb.Append("</script>");
    ClientScript.RegisterStartupScript(this.GetType(), "GridPrint", sb.ToString());
    GridView1.DataBind();
    }


    See the attached image. It's an output i have got using above mentioned code.


    Hope this is what you are looking for.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762894
    I tried this code also and many but i can't get desired result.

    I need headers should be fixed so that even i scroll the page i can see the headers.

    More over i need to print labels also so that i can print with date or any text.


Sign In to post your comments