因此,我正在编写90%完成的发票申请。我已经注意到在将XML文件加载到Datagridview后将其导出为PDF时,它会使PDF文档中的单元格加倍。如果我在不保存和加载XML的情况下将数据输入DGV,则不会发生这种情况。我一辈子都无法弄清楚为什么会这样做,因为在保存时似乎并没有向XML文件中写入更多内容,在加载时也没有写入更多内容。这仅在我导出为PDF时发生。也许我只是在看代码太久了。提供的任何帮助将不胜感激。谢谢!我将发布用于保存,加载和导出的代码...
我已经尝试过在加载,保存和导出功能中设置列数,但这只是增加单元数而不增加列数。
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.TableName = "Inventory";
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "xml";
if (sfd.ShowDialog() == DialogResult.OK)
{
invoice_DG.AllowUserToAddRows = false;
for (int i = 0; i < 6; i++)
{
if (invoice_DG.Columns[i].Visible) // Add's only Visible columns (if you need it)
{
string headerText = invoice_DG.Columns[i].HeaderText;
//headerText = Regex.Replace(headerText, "[-/, ]", "_");
DataColumn column = new DataColumn(headerText);
dt.Columns.Add(column);
}
}
foreach (DataGridViewRow DataGVRow in invoice_DG.Rows)
{
DataRow dataRow = dt.NewRow();
// Add's only the columns that you want
dataRow["QTY"] = DataGVRow.Cells["QTY"].Value;
dataRow["ITEM"] = DataGVRow.Cells["ITEM"].Value;
dataRow["COST"] = DataGVRow.Cells["COST"].Value;
dataRow["RETAIL"] = DataGVRow.Cells["RETAIL"].Value;
dt.Rows.Add(dataRow); //dt.Columns.Add();
}
DataSet ds = new DataSet();
ds.Tables.Add(dt);
//Finally the save part:
XmlTextWriter xmlSave = new XmlTextWriter(sfd.FileName, Encoding.UTF8);
xmlSave.Formatting = Formatting.Indented;
ds.DataSetName = "Data";
ds.WriteXml(xmlSave);
xmlSave.Close();
}
if(invoice_DG.AllowUserToAddRows == false)
{
invoice_DG.AllowUserToAddRows = true;
}
}
private void exportToPDFToolStripMenuItem_Click(object sender, EventArgs e)
{
string par_text = "";
SaveFileDialog save_Dialog = new SaveFileDialog();
save_Dialog.DefaultExt = "pdf";
if (save_Dialog.ShowDialog() == DialogResult.OK)
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
if (File.Exists(save_Dialog.FileName))
{
PdfWriter wr = PdfWriter.GetInstance(doc, new FileStream(save_Dialog.FileName, FileMode.Append));
}
else
{
PdfWriter wr = PdfWriter.GetInstance(doc, new FileStream(save_Dialog.FileName, FileMode.CreateNew));
}
doc.Open(); // Open Document to write
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
PdfPTable table = new PdfPTable(invoice_DG.Columns.Count);
table.DefaultCell.Padding = 3;
table.WidthPercentage = 100;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.DefaultCell.BorderWidth = 1;
iTextSharp.text.Font d_Text = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
//Add Headertext
foreach (DataGridViewColumn column in invoice_DG.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, d_Text));
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
table.AddCell(cell);
}
//Add Row Data
foreach (DataGridViewRow row in invoice_DG.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null)
{
Console.WriteLine("Adding cell: " + cell.Value.ToString());
table.AddCell(new Phrase(cell.Value.ToString(), d_Text));
}
}
}
par_text = invoice_TB.Text + "\n\n" + title_LB.Text + "\n\n";
Paragraph par = new Paragraph(par_text);
par.Alignment = Element.ALIGN_CENTER;
doc.Add(par);
doc.Add(table);
par_text = "\n\n" + this.oc_LB.Text + this.o_Cost.Text + "\n\n" + this.or_LB.Text + this.o_Retail.Text;
par.Alignment = Element.ALIGN_RIGHT;
par = new Paragraph(par_text);
doc.Add(par);
par.Alignment = Element.ALIGN_LEFT;
par_text = paid_BN.Text;
par = new Paragraph(par_text);
doc.Add(par);
doc.Close();
}
}
//********LOADING FROM XML to DATAGRIDVIEW (invoice_DG)**************
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
invoice_DG.AllowUserToAddRows = false;
DataSet ds = new DataSet();
ds.ReadXml(ofd.FileName);
invoice_DG.DataSource = ds.Tables["Inventory"];
for(int i = 0; i < ds.Tables["Inventory"].Columns.Count; i++)
{
if (ds.Tables["Inventory"].Rows[i][0].ToString() != null || ds.Tables["Inventory"].Rows[i][0].ToString() != "")
{
invoice_DG.Rows[i].Cells[0].Value = ds.Tables["Inventory"].Rows[i][0].ToString();
invoice_DG.Rows[i].Cells[1].Value = ds.Tables["Inventory"].Rows[i][1].ToString();
invoice_DG.Rows[i].Cells[2].Value = ds.Tables["Inventory"].Rows[i][2].ToString();
invoice_DG.Rows[i].Cells[3].Value = ds.Tables["Inventory"].Rows[i][3].ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine("********************\n" + ex.ToString() + "\n********************");
}
if (invoice_DG.AllowUserToAddRows == false)
{
invoice_DG.AllowUserToAddRows = true;
}
}
total_CostandRetail();
}