簡體   English   中英

PdfPTable 構造函數中的列數必須大於零

[英]The number of columns in PdfPTable constructor must be greater than zero

protected void pdf(DataTable dt, string str)
        {
            Document doc = new Document(iTextSharp.text.PageSize.A4, 10, 22, 34, 34);
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath+"sdad.pdf", FileMode.Create));
            doc.Open();
            doc.Add(new Paragraph("Table"));
            GridView dataGridview = new GridView();


            dataGridview.ShowHeaderWhenEmpty = true;
            dataGridview.AllowPaging = false;
            dataGridview.DataSource = dt;
            dataGridview.DataBind();
            PdfPTable table = new PdfPTable(dataGridview.Columns.Count);
            for (int j = 0; j < dataGridview.Columns.Count; j++)
            {
                table.AddCell(new Phrase(dataGridview.Columns[j].HeaderText));
            }
            table.HeaderRows = 1;
            for (int j = 0; j < dataGridview.Rows.Count;j++)
            {
                for (int k = 0; k < dataGridview.Columns.Count; k++)
                {
                    if (dataGridview.Rows[j].Cells[k].Text  != null)
                    {
                        table.AddCell(new Phrase(dataGridview.Rows[j].Cells[k].Text.ToString()));
                    }
                }
            }
            doc.Add(table);
            doc.Close();

        }

這是使用 Isharp 文本創建 PDF 的代碼。 但我收到錯誤 PdfPTable 構造函數中的列數必須大於零。 請任何人都可以幫助解決此錯誤。 提前致謝。

就我而言,我有這個明顯“無效”的 html:

 <table class="table-bordered" style="width: 100%; margin-top: 10px">
 </table>

IE。 沒有行的表。

我遇到了這個問題,我找到了解決方案,您可以添加

AutoGenerateColumns="False" ItemsSource="{Binding}在您的 xml 中(在 DataGrid 標記中)。那么您必須指定 dataGrid.Columns。

<DataGrid  Name="grdTransaksi" HorizontalAlignment="Left"Margin="10,94,0,0" VerticalAlignment="Top" AutoGenerateColumns="False" ItemsSource="{Binding}">
     <DataGrid.Columns>
       <DataGridTextColumn Header="Tanggal" Binding="{Binding tanggal}" />
       <DataGridTextColumn Header="Kode Akun" Binding="{Binding KodeAkun}" />
     </DataGrid.Columns>
</DataGrid>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM