We can have any type of column in a Grid View by using a Template column like Link, Drop Down, Radio button and check box list. So how to export all those into a PDF file? We will learn how to do that here.
I got a business requirement to design a form as in the following and export this into a PDF.
The following is my SQL Server Table in Design mode:
Script of my table
- CREATE TABLE [dbo].[Employee]
- (
- [ID] [int] IDENTITY(1, 1) NOT NULL,
- [Name] [varchar](50) NULL,
- [Email] [varchar](500) NULL,
- [Country] [varchar](50) NULL
- ) ON [PRIMARY] GO
Here, to export the Grid View Data to a PDF we need to add a reference of iTextSharp as in the following:
aspx code
Now the following is my aspx code:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ExportGridViewTemplateColumnToPDF.Default" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Export Grid View Template Column (Link, Drop Down List, Radio Button, Check Box List) To PDF</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:GridView ID="GridView1" HeaderStyle-BackColor="#666666" HeaderStyle-ForeColor="White" Font-Names="Verdana"
- RowStyle-BackColor="#E4E4E4" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" ForeColor="#284775" Font-Names="Verdana" />
- <Columns>
- <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30px">
- <ItemStyle Width="30px"></ItemStyle>
- </asp:BoundField>
- <asp:TemplateField HeaderText="Name">
- <ItemTemplate>
- <asp:HyperLink ID="lnkName" runat="server" NavigateUrl="#" Text='<%# Eval("Name") %>'>
- </asp:HyperLink>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Email">
- <ItemTemplate>
- <asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("Email") %>'>
- </asp:TextBox>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Country">
- <ItemTemplate>
- <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Country") %>'>
- </asp:TextBox>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Reporting Manager">
- <ItemTemplate>
- <asp:DropDownList ID="ddlManager" runat="server">
- <asp:ListItem Text="Shambhu Sharma" Value="Shambhu Sharma" />
- <asp:ListItem Text="Amol Malhotra" Value="Amol Malhotra" />
- <asp:ListItem Text="Manu Khanna" Value="Manu Khanna" />
- </asp:DropDownList>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Designation" ItemStyle-ForeColor="Red" ItemStyle-Font-Size="10pt">
- <ItemTemplate>
- <asp:CheckBoxList ID="chkDesignation" runat="server" RepeatColumns="3" RepeatDirection="Horizontal">
- <asp:ListItem Text="Softwate Developer" Value="Softwate Developer" Selected="True" />
- <asp:ListItem Text="Technical Analyst" Value="Technical Analyst" />
- <asp:ListItem Text="Consultant" Value="Consultant" />
- <asp:ListItem Text="Solution Architect" Value="Solution Architect" />
- <asp:ListItem Text="Project Manager" Value="Project Manager" />
- </asp:CheckBoxList>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Experience" ItemStyle-ForeColor="Blue" ItemStyle-Font-Size="10pt">
- <ItemTemplate>
- <asp:RadioButtonList ID="rblExperience" runat="server" RepeatColumns="2" RepeatDirection="Horizontal">
- <asp:ListItem Text="1-2 Years" Value="1-2 Years" Selected="True" />
- <asp:ListItem Text="2-4 Years" Value="2-4 Years" />
- <asp:ListItem Text="4-6 Years" Value="4-6 Years" />
- <asp:ListItem Text="10+ Years" Value="10+ Years" />
- </asp:RadioButtonList>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <EditRowStyle BackColor="#999999" />
- <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True"></HeaderStyle>
- <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="#F7F6F3" ForeColor="#333333"></RowStyle>
- <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
- <SortedAscendingCellStyle BackColor="#E9E7E2" />
- <SortedAscendingHeaderStyle BackColor="#506C8C" />
- <SortedDescendingCellStyle BackColor="#FFFDF8" />
- <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
- </asp:GridView>
- <br />
- <asp:Button ID="btnGridViewExport" runat="server" Text="Export To PDF" OnClick="btnGridViewExport_ExportToPDF" />
- </div>
- </form>
- </body>
- </html>
aspx.cs code
Now my aspx.cs code is:
web.config file
The following is my web.config file where I define my connection string:
- <?xml version="1.0"?>
- <!--
- For more information on how to configure your ASP.NET application, please visit
- http:
- -->
- <configuration>
- <system.web>
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5" />
- </system.web>
- <connectionStrings>
- <add name="RConnection" connectionString="Server=INDIA\MSSQLServer2k8;database=TestDB;UID=sa; pwd=india;"/>
- </connectionStrings>
- </configuration>
Test
Now run the application.
Now you can change the value from Drop Down List, Radio Button or Check Box List as in the following:
You can make change in your from and do the export again as in the following: