如何将选定的网格视图详细信息转换为pdf?得到错误
本文关键字:pdf 转换 错误 详细信息 视图 网格 | 更新日期: 2023-09-27 18:04:29
代码1:
private void GetPaymentReport()
{
try
{
string todate = txttodate.Text;
string[] todatesplt = todate.Split('-');
int todatemnth = Convert.ToInt32(todatesplt[0]);
int todatedate = Convert.ToInt32(todatesplt[1]);
int todateyr = Convert.ToInt32(todatesplt[2]);
string fromdate = txtfromdate.Text;
string[] frmdatesplt = fromdate.Split('-');
int frmdatemnth = Convert.ToInt32(frmdatesplt[0]);
int frmdatedate = Convert.ToInt32(frmdatesplt[1]);
int frmdateyr = Convert.ToInt32(frmdatesplt[2]);
DateTime start = new DateTime(frmdateyr, frmdatemnth, frmdatedate);
DateTime end = new DateTime(todateyr, todatemnth, todatedate);
int loanpaymentid = Convert.ToInt32(Session["Loanid"].ToString());
var query = from p in mortgageentity.Payments
join D in mortgageentity.Debit_Method on p.Debit_Method_ID equals D.Debit_Method_ID
join pt in mortgageentity.Payment_Type on p.Payment_Type_ID equals pt.Payment_Type_ID
where (p.Client_Pmt_Date >= start) && (p.Client_Pmt_Date <= end) && (p.Loan_ID == loanpaymentid)orderby p.Client_Pmt_Date descending
select new
{
p.Pmt_ID,
p.Loan_ID,
p.Client_Pmt_Date,
p.MtgSvr_Pmt_Start_Date2,
D.Debit_Method_Desc,
p.Total_Debit_Amt,
p.CreditAmt,
p.LenderAmt,
pt.Payment_Type_Desc,
p.Return_Code,
p.Returned_Date
};
grdviewPayments.DataSource = query.ToList();
grdviewPayments.DataBind();
}
catch { }
}
代码2:protected void btnemail_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);
grdviewPayments.AllowPaging = false;
grdviewPayments.DataBind();
grdviewPayments.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();
}
如上所述,我有code1从gridview格式的数据库中获取数据,code2将gridview数据转换为PDF格式。但我的问题是得到错误,如"控制'ctl00_ContentPlaceHolder1_TabContainer1_TabPanel1_Paymentreport_grdviewPayments'类型'GridView'必须放置在一个表单标签与runat=server."在code2位置:grdviewPayments.RenderControl(hw);
请帮帮我。问候。
也许你需要合并StringBuilder ?
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
grdviewPayments.RenderControl(hw);
StringReader sr = new StringReader(hw.ToString());
我猜是因为我在这页上看到的东西:
http://blogs.x2line.com/al/articles/859.aspx