Click here to Skip to main content
13,508,910 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have two pages which contains gridviews i have two buttons on each page which will help to print the gridviews as Excel and Pdf

one page contains normal gridview one page contains multi header

I created Multi header by using the faallowing code

public static void GetMultiRowHeader(GridViewRowEventArgs e, SortedList GetCels)
       if (e.Row.RowType == DataControlRowType.Header)
           GridViewRow row;
           IDictionaryEnumerator enumCels = GetCels.GetEnumerator();
           row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
           while (enumCels.MoveNext())
               String[] count = enumCels.Value.ToString().Split(Convert.ToChar(","));
               TableCell Cell;
               Cell = new TableCell();
               Cell.RowSpan = Convert.ToInt16(count[2].ToString());
               Cell.ColumnSpan = Convert.ToInt16(count[1].ToString());

               Cell.Controls.Add(new LiteralControl(count[0].ToString()));
               Cell.HorizontalAlign = HorizontalAlign.Center;
               Cell.ForeColor = System.Drawing.Color.White;
           e.Row.Parent.Controls.AddAt(0, row);


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        //Everytime you want to add new rows header, you creat new formatcells variable
        //Dim formatCells As New SortedList
        SortedList formatCells = new SortedList();
        //Format cells format:"
        //formatCells.Add(<column number>, <header Name,number of column to colspan, number of row to rowspan>)

        //formatCells.Add("1", "ROW SPAN,1,2");
        //formatCells.Add("2", "TopGroup,4,1");
        // Dim formatcells2 As New SortedList
        SortedList formatcells2 = new SortedList();
        formatcells2.Add("1", ",9,1");
        formatcells2.Add("10", "Ground Status (Extent in Sq.Mtrs),2,1");
        formatcells2.Add("12", "Court case Status,2,1");
        formatcells2.Add("14", "Allotted to Govt Depts(others and Exemptions),2,1");
        formatcells2.Add("16", ",2,1");
       // formatcells2.Add("4", ",1,1");
        //formatcells2.Add("5", "Qualification,3,1");
        GetMultiRowHeader(e, formatcells2);
        GetMultiRowHeader(e, formatCells);


I Generated Excel by using fallowing code

protected void ImageButtonExcel_Click(object sender, ImageClickEventArgs e)
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "ulcact.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        //Change the Header Row back to white color
        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
        //Applying stlye to gridview header cells
        for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
            GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
        int j = 1;
        //This loop is used to apply stlye to cells based on particular row
        foreach (GridViewRow gvrow in GridView1.Rows)
            //gvrow.BackColor = Color.White;
            if (j <= GridView1.Rows.Count)
                if (j % 2 != 0)
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");

I created Pdf by using the fallowing code

protected void ImageButtonPdf_Click(object sender, ImageClickEventArgs e)
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=ulcact.pdf");
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.HeaderRow.Style.Add("width", "15%");
        GridView1.HeaderRow.Style.Add("font-size", "10px");
        GridView1.Style.Add("text-decoration", "none");
        GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        GridView1.Style.Add("font-size", "8px");
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

The problems i faced are

1.while in the administration mode i have two more columns in the gridview for selecting and updating i wanted to exclude them from printing as excel
(i,e simply i wanted to exclude first two colums from excel when generating Excel sheet) the multi header form i am getting the fallowing error (i used iTextSharp for generating Pdf

The number of columns in PdfPTable constructor must be greater than zero.

Line 129: PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
Line 130: pdfDoc.Open();
Line 131: htmlparser.Parse(sr);
Line 132: pdfDoc.Close();
Line 133: Response.Write(pdfDoc);

Error Line:Source File: c:\Users\ARTISAN\Documents\Visual Studio 2008\WebSites\ULCHYD\ulcact.aspx.cs Line: 131
Posted 17-Oct-11 19:36pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

i am facing the same problem and i can't solve it
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

krishna_goluguri 19-Oct-11 0:48am
i already have that code i am already exported as pdf and Excel the problem is i wanted to exclude 2 columns from grid in excel sheet (ie Select and Edit) and i wanted to generate pdf for mufti header grid(i,e header contains two rows)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180417.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100