Here is the updated code for exporitng gridview to pdf along with the charts
Step 1: Place this gridview and button on your aspx page :
<asp:Button ID="btnExportGridView" runat="server" Text="ExportChart" OnClick="btnExportGridView_Click" />
<asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false"
onrowcreated="gvData_RowCreated" onrowdatabound="gvData_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Username" DataField="Username" ItemStyle-Width="120px" />
<asp:TemplateField HeaderText="marks Report">
<ItemTemplate>
<asp:Chart ID="Chart2" runat="server" Width="250px" Height="200px">
<Series>
<asp:Series Name="Series1" LegendText="LEVEL 1" IsValueShownAsLabel="false" ChartArea="ChartArea1"
MarkerBorderColor="#DBDBDB" XValueMember="col1" YValueMembers="col2">
</asp:Series>
</Series>
<ChartAreas>
<asp:ChartArea Name="ChartArea1">
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Step 2: Binding Sample Data to GridView :
DataTable dtData;
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void BindData()
{
dtData = GetData();
gvData.DataSource = dtData;
gvData.DataBind();
}
private DataTable GetData()
{
DataTable dtDataTmp = new DataTable();
dtDataTmp.Columns.Add("Username", typeof(string));
dtDataTmp.Columns.Add("Marks", typeof(DataTable));
DataTable dt1 = new DataTable();
dt1.Columns.Add("col1", typeof(string));
dt1.Columns.Add("col2", typeof(decimal));
DataTable dt2 = new DataTable();
dt2.Columns.Add("col1", typeof(string));
dt2.Columns.Add("col2", typeof(decimal));
dt1.Rows.Add("Math", 40);
dt1.Rows.Add("Science", 65);
dt1.Rows.Add("English", 74);
dt1.AcceptChanges();
dt2.Rows.Add("Math", 45);
dt2.Rows.Add("Science", 60);
dt2.Rows.Add("English", 30);
dt2.AcceptChanges();
dtDataTmp.Rows.Add("Rohit", dt1);
dtDataTmp.Rows.Add("Mohit", dt2);
dtDataTmp.AcceptChanges();
return dtDataTmp;
}
protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dtTmp = (DataTable)dtData.Rows[e.Row.RowIndex]["Marks"];
Chart chart = (Chart)e.Row.FindControl("Chart2");
chart.DataSource = dtTmp;
chart.DataBind();
}
}
Step 3: Code to Export the Gridview alongwith chart to pdf format using iTextSharp :
protected void btnExportGridView_Click(object sender, EventArgs e)
{
Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);
using (MemoryStream output = new MemoryStream())
{
PdfWriter wri = PdfWriter.GetInstance(doc, output);
doc.Open();
GridViewRow gvr = null;
PdfPTable tab = new PdfPTable(2);
PdfPCell[] cells = null;
Chart chrt = null;
Random rnd = new Random();
string img_path = "";
List<string> lstImgs = new List<string>();
for (int i = 0; i < gvData.Rows.Count; i++)
{
gvr = gvData.Rows[i];
PdfPCell c1 = new PdfPCell();
PdfPCell c2 = new PdfPCell();
for (int j = 0; j < gvData.Rows[i].Cells.Count; j++)
{
c1 = new PdfPCell();
c2 = new PdfPCell();
c1.AddElement(new Paragraph(gvr.Cells[0].Text));
chrt = (Chart)gvr.FindControl("Chart2");
img_path = Server.MapPath("Images/img_" + rnd.NextDouble() + ".jpg");
chrt.SaveImage(img_path);
c2.AddElement(iTextSharp.text.Image.GetInstance(img_path));
lstImgs.Add(img_path);
}
cells = new PdfPCell[] { c1, c2 };
tab.Rows.Add(new PdfPRow(cells));
}
doc.Add(tab);
doc.Close();
foreach (string img in lstImgs)
{
if (File.Exists(img))
File.Delete(img);
}
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=testpdf.pdf");
Response.BinaryWrite(output.ToArray());
Response.End();
Response.Flush();
Response.Clear();
}
}
Enjoy !!