导出到pdf gridview是空的.net

时间:2014-08-06 12:45:38

标签: c# asp.net pdf gridview

当我尝试将gridview导出为pdf时,我唯一得到的是一个空表。这是我使用的代码:

protected void ExportPDF_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition",
     "attachment;filename=GridViewExport.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    GridView1.AllowPaging = false;
    GridView1.DataBind();
    GridView1.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End(); 
}
public override void VerifyRenderingInServerForm(Control control)
{
    //required to avoid the runtime error "  
    //Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server." 

} 

gridview绑定如下:

  string from = Request.QueryString["FromDate"];
        string to = Request.QueryString["ToDate"];

        TextBox1.Text = from;
        TextBox2.Text = to;


        string strCon = System.Web
          .Configuration
          .WebConfigurationManager
          .ConnectionStrings["connstring"].ConnectionString;

        String userSearchCommand = "my query is inserted here";

        SqlDataSource dataSource = new SqlDataSource(strCon, userSearchCommand);
        dataSource.SelectCommandType = SqlDataSourceCommandType.Text;
        dataSource.ID = "all1";
        GridView1.DataSource = dataSource;
        GridView1.DataBind();

关键是我在尝试导出时尝试绑定gridview。 有人可以帮我吗?

0 个答案:

没有答案