2
Answers

How to export gridview with dynamiclly added header

I have gridview
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"  ForeColor="#333333" GridLines="None" DataKeyNames="proizvodID,kategorijaID" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"  ShowFooter="True"   OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated" Width="600
        px"   >
    <AlternatingRowStyle BackColor="#F7F7F7" Font-Bold="True" Font-Size="11pt" />
    <Columns>
        <asp:BoundField DataField="proizvodID" HeaderText="proizvodID" ReadOnly="True" SortExpression="proizvodID" Visible="False" />
        <asp:TemplateField HeaderText="naziv">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="detalji" Font-Underline="false" Text='<%# Eval("naziv") %> '  CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ></asp:LinkButton>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="Button1" runat="server" Text="Vrati se na pretragu" OnClick="Button1_Click" />
                <asp:Button ID="Button2" runat="server" Text="Potvrdi kupvinu" OnClick="Button2_Click"/>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="opis" HeaderText="opis" SortExpression="opis" Visible="False" />
        <asp:BoundField DataField="cena" HeaderText="cena" SortExpression="cena" />
        <asp:BoundField DataField="kategorijaID" HeaderText="kategorijaID" SortExpression="kategorijaID" Visible="False" />
        <asp:TemplateField HeaderText="kolicina" SortExpression="kolicina">
            <FooterTemplate>
                <asp:Label ID="Label1" runat="server" Text="UKUPNO" Font-Bold="True" Font-Size="Large" ForeColor="White"></asp:Label>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("kolicina") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="iznos">
            <ItemTemplate>
                <asp:Label ID="iznos" runat="server" Text='<%# Eval("iznos") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="Label2" runat="server" Text="Label" Font-Bold="True" Font-Size="Large" ForeColor="White"></asp:Label>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#B5C7DE" BorderStyle="None" ForeColor="#4A3C8C" Font-Bold="True" Font-Size="11pt" />
    <HeaderStyle BackColor="#B5C7DE" Font-Bold="True" ForeColor="#F7F7F7" Font-Size="11pt" HorizontalAlign="Center" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" Font-Bold="True" Font-Size="11pt" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <SortedAscendingCellStyle BackColor="#F4F4FD" />
        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
        <SortedDescendingCellStyle BackColor="#D8D8F0" />
        <SortedDescendingHeaderStyle BackColor="#3E3277" />
    </asp:GridView>
in code I dynamiclly added header rows
  protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            DataClasses1DataContext kori = new DataClasses1DataContext();
            var kor = (from k in kori.Korisnicis where Session["log"].ToString() == k.korisnickoime select k).FirstOrDefault();
            if (e.Row.RowType == DataControlRowType.Header)
            {
                // dodavanje extra reda u heder
                GridViewRow header3 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow header4= new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow header = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow header1 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow header2= new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow header0 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                TableCell cel3 = new TableCell();
                cel3.ColumnSpan = 4;
                cel3.Text = "ziro racun : 160-79750-28";
                cel3.HorizontalAlign = HorizontalAlign.Justify;
                header3.Cells.Add(cel3);
                GridView1.Controls[0].Controls.AddAt(0, header3);

                TableCell cel4 = new TableCell();
                cel4.ColumnSpan = 4;
                cel4.Text = "Pib 101735494 ";
                cel4.HorizontalAlign = HorizontalAlign.Justify;
                header4.Cells.Add(cel4);
                GridView1.Controls[0].Controls.AddAt(0, header4);
                
                TableCell cel = new TableCell();
                cel.ColumnSpan = 2;
                cel.Text = "11000 Beograd ";
                cel.HorizontalAlign = HorizontalAlign.Justify;
                header.Cells.Add(cel);
                TableCell celgr = new TableCell();
                celgr.ColumnSpan = 2;
                celgr.Text = kor.grad;
                celgr.HorizontalAlign = HorizontalAlign.Justify;
                header.Cells.Add(celgr);
                GridView1.Controls[0].Controls.AddAt(0, header);

                TableCell cel1 = new TableCell();
                cel1.ColumnSpan = 2;
                cel1.Text = "109 nova 18";
                cel1.HorizontalAlign = HorizontalAlign.Justify;
                TableCell celadr = new TableCell();
                celadr.ColumnSpan = 2;
                celadr.Text = kor.adresa;
                celadr.HorizontalAlign = HorizontalAlign.Justify;
                header1.Cells.Add(cel1);
                header1.Cells.Add(celadr);
                GridView1.Controls[0].Controls.AddAt(0, header1);
              
                TableCell cel2 = new TableCell();
                cel2.ColumnSpan = 2;
                cel2.Text = "verona company";
                cel2.HorizontalAlign = HorizontalAlign.Justify;
                TableCell celk = new TableCell();
                celk.ColumnSpan = 2;
                celk.Text = kor.ime+" "+kor.prezime + brRac ;
                celk.HorizontalAlign = HorizontalAlign.Justify;
                header2.Cells.Add(cel2);
                header2.Cells.Add(celk);
                GridView1.Controls[0].Controls.AddAt(0, header2);

                TableCell celprod = new TableCell();
                celprod.ColumnSpan = 2;
                celprod.Text = "prodavac";
                celprod.HorizontalAlign = HorizontalAlign.Justify;
                TableCell celkup = new TableCell();
                celkup.ColumnSpan = 2;
                celkup.Text = "kupac";
                header0.Cells.Add(celprod);
                header0.Cells.Add(celkup);
                celkup.HorizontalAlign = HorizontalAlign.Justify;
                GridView1.Controls[0].Controls.AddAt(0, header0);
                  

            }
I am use next code to export gridview to pdf Its works but I can't positioning gridview on pdf document It is always on the left top corner  positon If I use other code and create table and table write to doc I can't got header rows from gridview
GridView1.DataBind();
                   StringWriter stringWriter2 = new StringWriter();
                   HtmlTextWriter htmlTextWriter2 = new HtmlTextWriter(stringWriter2);
                   GridView1.RenderControl(htmlTextWriter2);

                   StringReader sr2 = new StringReader(stringWriter2.ToString());
                   Document doc2 = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
                
                   PdfWriter writer2 = PdfWriter.GetInstance(doc2, Response.OutputStream);
                   doc2.Open();
                
                  
                 XMLWorkerHelper.GetInstance().ParseXHtml(writer2, doc2, sr2);
                  doc2.Close();

                  Response.ContentType = "application/pdf";
                  Response.AddHeader("content-disposition", "attachment;filename=Racun.pdf");
                  Response.Cache.SetCacheability(HttpCacheability.NoCache);
 Thanks in advance
 
 
 
 
 
Answers (2)