Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I wont to create pdf file from gridview in asp.net.I convert it in cvs format but not in pdf,for cvs i use this code:

HttpContext context = HttpContext.Current;

      context.Response.Clear();
      context.Response.ContentType = "text/csv";
      context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
      //rite column header names
      for (int i = 0; i < data.Columns.Count - 1; i++)
      {
          if (i > 0)
          {
              context.Response.Write(,);
          }
          context.Response.Write(data.Columns[i].ColumnName);
      }
      context.Response.Write(Environment.NewLine);

      //Write data
      foreach (DataRow row in data.Rows)
      {
          for (int i = 0; i < data.Columns.Count - 1; i++)
          {
              if (i > 0)
              {
                  context.Response.Write(",");
              }
              context.Response.Write(row.ItemArray[i]);
          }
          context.Response.Write(Environment.NewLine);
      }
      context.Response.End()
Posted
Updated 22-May-11 22:46pm
v2

1 solution

 
Share this answer
 
Comments
vineesha 23-May-11 7:05am    
I use that code but following error is occur:
Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
Prerak Patel 23-May-11 7:07am    
Check the tag of GridView1 in your .aspx file, and add runat="server" if it is not there.
vineesha 27-May-11 9:10am    
All these are same in my .aspx file but error is come
gnjan 1-Jun-11 7:10am    
you have to add these lines below your code
public override void VerifyRenderingInServerForm(Control control)
{
return;
}

now I suppose it will work --

Gunjan

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900