简体   繁体   中英

Exporting a grid view data in to pdf

i am getting error:Control 'MainContent_dgvRpt' of type 'GridView' must be placed inside a form tag with runat=server.

asp code:

<asp:GridView ID="dgvRpt" runat="server" Width="900px" CssClass="Grid" OnRowDataBound="dgvShowRpt_RowDataBound">
<RowStyle HorizontalAlign="Left" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="black" Font-Names="Arial" />
<PagerStyle BackColor="#CDCDCD" ForeColor="White" HorizontalAlign="Center" Font-Names="Arial" />
<HeaderStyle BackColor="#CDCDCD" Font-Bold="True" ForeColor="black" Font-Names="Arial" />

<HeaderStyle CssClass="GridHeader" Font-Names="Arial"></HeaderStyle>
<AlternatingRowStyle CssClass="GridAtlItem" />
</asp:GridView>.

c# code:

GameLib.Utilities.ExportGrid(dgvRpt, "pdf", GameLib.Reports.ReportName);

     if (ExportType == "pdf")
                {
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.Buffer = true;
                    HttpContext.Current.Response.ContentType = "application/pdf";
                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + Filename + ".pdf");

                    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    StringWriter sw = new StringWriter();
                    HtmlTextWriter hw = new HtmlTextWriter(sw);


                    gv.AllowPaging = false;
                    gv.HeaderRow.ForeColor = System.Drawing.Color.Black;
                    gv.FooterRow.ForeColor = System.Drawing.Color.Black;

                    gv.HeaderRow.Style.Add("font-Color", "Black");
                    gv.HeaderRow.Style.Add("font-size", "13px");
                    gv.HeaderRow.Style.Add("text-decoration", "none");
                    gv.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");

                    gv.Style.Add("font-Color", "Black");
                    gv.Style.Add("text-decoration", "none");
                    gv.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
                    gv.Style.Add("font-size", "11px");
                    gv.ForeColor = System.Drawing.Color.Black;

                    gv.RenderControl(hw);
                    StringReader sr = new StringReader(sw.ToString());
                    Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);

                    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                    PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
                    pdfDoc.Open();

                    htmlparser.Parse(sr);
                    pdfDoc.Close();
                    HttpContext.Current.Response.Write(pdfDoc);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();

getting error in gv.RenderControl(hw); but i inserted code inside runat=server. Please help me sort the problem.Thanx in advance

You have to tell the compiler that the control is rendered explicitly by overriding the VerifyRenderingInServerForm event. Like:

    public override void VerifyRenderingInServerForm(Control control)
    {
       //
    }

http://www.codeproject.com/Questions/381483/Control-GridView1-of-type-GridView-must-be-placed

I've the same logic to export a GridView to an excel, but the gridview exists only in code-behind. Here is my code:

protected void ExportToExcel(DataTable dt)
        {
            //Create a dummy GridView
            GridView GridView1 = new GridView();
            GridView1.ShowHeaderWhenEmpty = true;
            GridView1.AllowPaging = false;
            GridView1.DataSource = dt;
            GridView1.DataBind();            

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;

            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            List<int> decimalModeColumnsIndexes = new List<int>();

            // Set the column's header with the caption of the Column in the DataTable
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                GridView1.HeaderRow.Cells[i].Text = dt.Columns[i].Caption;                
            }

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                //Apply text style to each cell
                if (decimalModeColumnsIndexes.Count != 0)
                {
                    for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
                    {
                        if (decimalModeColumnsIndexes.Count(colIndex => colIndex == j) == 1)
                        {
                            GridView1.Rows[i].Cells[j].Attributes.Add("class", "decimalmode");
                        }
                        else
                        {
                            GridView1.Rows[i].Cells[j].Attributes.Add("class", "textmode");
                        }
                    }
                }
                else
                {
                    GridView1.Rows[i].Attributes.Add("class", "textmode");
                }

            }

            String fileName = dt.TableName;
            fileName = String.Format("{0}_{1}", fileName, DateTime.UtcNow);

            fileName = fileName.Replace(':', '-');
            fileName = fileName.Replace(' ', '-');


            GridView1.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { mso-number-format:\@; } .decimalmode { mso-number-format:""0\.00""; } </style>";


            HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", fileName));
            HttpContext.Current.Response.Charset = "";
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.Write(style);
            HttpContext.Current.Response.Output.Write(sw.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

        }

you can use this:modifying the previous code.

protected void ExportToPdf(DataTable dt)
            {
                GridView GridView1 = new GridView();
                GridView1.ShowHeaderWhenEmpty = true;
                GridView1.AllowPaging = false;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + "ggggggggg" + ".pdf");

                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);


                GridView1.AllowPaging = false;
                GridView1.HeaderRow.ForeColor = System.Drawing.Color.Black;
                GridView1.FooterRow.ForeColor = System.Drawing.Color.Black;

                GridView1.HeaderRow.Style.Add("font-Color", "Black");
                GridView1.HeaderRow.Style.Add("font-size", "13px");
                GridView1.HeaderRow.Style.Add("text-decoration", "none");
                GridView1.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");

                GridView1.Style.Add("font-Color", "Black");
                GridView1.Style.Add("text-decoration", "none");
                GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
                GridView1.Style.Add("font-size", "11px");
                GridView1.ForeColor = System.Drawing.Color.Black;

                GridView1.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);

                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
                pdfDoc.Open();

                htmlparser.Parse(sr);
                pdfDoc.Close();
                HttpContext.Current.Response.Write(pdfDoc);
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM