I am working in ASP.Net project.
I have one gridview and data is bound from codebehind cs file. In some of the columns I am showing them links to open popup & also data.
Now my task is to print the gridview but i dont want to show those licks in print. It should print only data that is available otherwise blank cell.
My code is as below:
protected void btnPrint_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
grdAudit.GridLines = GridLines.Both;
table.GridLines = grdAudit.GridLines;
if (grdAudit.HeaderRow != null)
{
PrepareControlForExport(grdAudit.HeaderRow);
table.Rows.Add(grdAudit.HeaderRow);
}
foreach (GridViewRow row in grdAudit.Rows)
{
row.BorderColor = System.Drawing.Color.Black;
row.BorderWidth = 1;
int cnt = row.Cells.Count;
PrepareControlForExport(row);
for (int i = 0; i < cnt; i++)
{
row.Cells[i].BorderColor = System.Drawing.Color.Black;
row.Cells[i].BorderWidth = 1;
}
table.Rows.Add(row);
}
if (grdAudit.FooterRow != null)
{
PrepareControlForExport(grdAudit.FooterRow);
table.Rows.Add(grdAudit.FooterRow);
}
table.RenderControl(htw);
string data = sw.ToString();
table.Attributes["runat"] = "server";
table.Attributes["class"] = "print";
table.Attributes["rule"] = "all";
table.BorderColor = System.Drawing.Color.Black;
table.GridLines = GridLines.Both;
table.ID = "grdTable";
ContentPlaceHolder pageContent = Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
HtmlControl pageC = pageContent.FindControl("divPrint") as HtmlControl;
pageC.Controls.Add(table);
StringBuilder sb = new StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload = new function(){");
sb.Append("var prtContent = document.getElementById('" + table.ClientID + "');");
sb.Append("prtContent.border = \"1px solid #000\";");
sb.Append("var printWin = window.open('', '','left=0,top=0,width=1000,height=600,status=0,scrollbars=1');");
sb.Append("printWin.document.write(prtContent.outerHTML);");
sb.Append("printWin.document.close();");
sb.Append("printWin.focus();");
sb.Append("printWin.print();");
sb.Append("printWin.close();};");
sb.Append("};");
sb.Append("</script>");
ScriptManager.RegisterStartupScript(btnPrint, this.GetType(), "PrintNewButton", sb.ToString(), false);
}
}
}
private void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if(current.Visible == false)
control.Controls.Remove(current);
else if (current is LinkButton)
{
control.Controls.Remove(current);
}
else if (current is HtmlAnchor )
{
control.Controls.Remove(current);
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
else if (current is HiddenField)
{
control.Controls.Remove(current);
string text =(current as HiddenField).Value;
if(text != "Select Region")
control.Controls.AddAt(i, new LiteralControl((current as HiddenField).Value ));
}
if (current.HasControls())
{
PrepareControlForExport(current);
}
}
}
This code is working fine except below issues:
1. in print it shows the border for only those cells which have data otherwise it shows as plane paper w/o any gridlines
2.sometimes i get jaavscript error while registering the script in below line
ScriptManager.RegisterStartupScript(btnPrint, this.GetType(), "PrintNewButton", sb.ToString(), false);
It shows error in scriptresource.axd file at line (syntax may be wrong written here)
documnet.getelementbytag("HEAD")[0].appendshild(scriptelement)
Please help me.
Thanks & Regards,
Anu