我正在尝试将数据作为DataTable
的形式推送到函数中,而在另一种形式上,我有一个DataGridView
,其中包含该函数所必需的数据。
我有DataGridView invoice_data;
,我试过This Link
如下,
DataTable id = (DataTable)(invoice_data.DataSource);
现在网格表总共有0-6列。
现在这个功能,
public static string VPfn_GenInvoicePdf(string[] ivdata, DataTable ida)
{
StringBuilder sb = new StringBuilder();
try
{
using (System.IO.StreamReader Reader = new System.IO.StreamReader("inv.tpl"))
{
StringBuilder Sb = new StringBuilder();
string fileContent = Reader.ReadToEnd();
sb.Append(fileContent);
sb.AppendLine("<h2 class='name'>"+ VPfunctions.VPfn_N2ID_customer(ivdata[1]) +"</h2>");
sb.AppendLine("<div class='address'>"+ VPfunctions.VPfn_CusAddr(Int16.Parse(ivdata[1]))+"</div>");
sb.AppendLine("<div class='email'><a href='mailto:"+ VPfunctions.VPfn_CusEmail(Int16.Parse(ivdata[1]))+ "'>"+ VPfunctions.VPfn_CusEmail(Int16.Parse(ivdata[1])) + "</a></div>");
sb.AppendLine("</div> < div id = 'invoice' >");
sb.AppendLine("<h1>INVOICE"+ivdata[0]+"</h1>");
sb.AppendLine("<div class='date'>Date of Invoice:"+DateTime.Today+"</div>");
sb.AppendLine("<div class='date'>Due Date:"+ DateTime.Today.AddDays(30)+"</div>");
sb.AppendLine("</div></ div >< table border = '0' cellspacing = '0' cellpadding = '0' >< thead >< tr > ");
sb.AppendLine("<th class='no'>#</th><th class='desc'>DESCRIPTION</th><th class='unit'>UNIT PRICE</th><th class='qty'>QUANTITY</th><th class='vat'>VAT</th><th class='total'>TOTAL</th></tr></thead><tbody>");
foreach(DataGridViewRow x in ida.Rows)
{
sb.AppendLine("<tr>");
sb.AppendLine("<td class='no'>"+x.Cells[0].ToString()+"</td>");
sb.AppendLine("<td class='desc'><h3>"+x.Cells[1].ToString()+"</h3></td>");
sb.AppendLine("<td class='unit'>₹"+x.Cells[2].ToString()+"</td>");
sb.AppendLine("<td class='qty'>"+x.Cells[3].ToString()+"</td>");
sb.AppendLine("<td class='vat>"+x.Cells[5].ToString()+"%</td>");
sb.AppendLine("<td class='total'>₹"+x.Cells[6].ToString()+"</td>");
sb.AppendLine("</tr>");
}
sb.AppendLine("</tbody><tfoot><tr>");
sb.AppendLine("<td colspan='3'></td>< td colspan = '2' > SUBTOTAL </ td >< td >₹"+ ivdata[4].ToString()+ "</ td ></ tr ><tr> ");
sb.AppendLine("<td colspan='3'></td><td colspan='2'>TAX (4+1%)</td><td>+ ₹"+ivdata[5].ToString()+"</td></tr><tr>");
sb.AppendLine("<td colspan='3'></td><td colspan='2'>DISCOUNT (10%)</td><td>- ₹"+ivdata[6].ToString()+"</td></tr><tr>");
sb.AppendLine("<td colspan='3'></td><td colspan='2'>GRAND TOTAL</td><td>₹"+ivdata[7].ToString()+"</td></tr></tfoot></table>");
sb.AppendLine("<div id='thanks'>Thank you!</div><div id='notices'><div>NOTICE:</div><div class='notice'>A finance charge of 1.5% will be made on unpaid balances after 30 days.</div></div></main><footer>Invoice was created on a computer and is valid without the signature and seal.</footer></body></html>");
return sb.ToString();
}
}
catch(Exception ex)
{
MessageBox.Show("Invoice template loading failed! Please run updater! "+ex.Message);
return null;
}
这会以html格式创建发票,以便我可以轻松转换为pdf格式。
现在,出于测试目的,我运行了以下代码,我得到的索引超出范围错误。
private void button1_Click(object sender, EventArgs e)
{
string[] idata = new string[8];
idata[0] = tid.Text;
idata[1] = tcid.Text;
idata[2] = DateTime.Now.ToString();
idata[3] = null;
idata[4] = tst.Text;
idata[5] = tat.Text;
idata[6] = tdis.Text;
idata[7] = ttotal.Text;
DataTable id = (DataTable)(invoice_data.DataSource);
string invdata = VPInvoice.VPfn_GenInvoicePdf(idata, id);
webBrowser1.DocumentText = invdata;
}
错误:来自该捕获异常:对象引用未设置为对象的实例