C# PrintPreviewDialog 文档设置为 TabularReport,但未显示任何内容

发布于 2024-12-11 04:01:43 字数 666 浏览 0 评论 0原文

我有一个带有 PrintPreviewDialog 和 TabularReport 的表单。 我还有一个 DataGridView。

我从数据库填充一个 DataView 对象。现在不管如何,但它确实有效。 当我在 DataGridView 上使用 DataView 时,我可以看到正确的行和列。我使用这段代码:

DataView v = new DataView(dTable);
bindingSource1.DataSource = dTable;
grid.DataSource = bindingSource1;

但是当我在 TabularReport 上使用相同的 DataView,然后尝试在 PrintPreviewDialog 中查看它时,我得到一个空页面(除了标题之外)。我使用这个代码:

TabularReport1.DataView = v; // here v is the same DataView from the previous code block above
printPreview1.Document = TabularReport1;
//here of course i also set a button to load printPreview1.ShowDialog()

有谁知道为什么我得到一个空白页? 谢谢!

I have a Form with a PrintPreviewDialog and a TabularReport.
I also have a DataGridView.

I fill a DataView object from my DataBase. Nevermind now how, but it works.
When I use my DataView on the DataGridView, I can see the the right rows and coloumns. I use this code:

DataView v = new DataView(dTable);
bindingSource1.DataSource = dTable;
grid.DataSource = bindingSource1;

But when I use the same DataView on my TabularReport and then try to see it in my PrintPreviewDialog I get an empty page (besides an header). I use this Code:

TabularReport1.DataView = v; // here v is the same DataView from the previous code block above
printPreview1.Document = TabularReport1;
//here of course i also set a button to load printPreview1.ShowDialog()

Does anyone knows why Im getting a blank page?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩套路吗 2024-12-18 04:01:43

您还必须设置表格的边距,这里有一个示例代码,如何使用数据设置边距并使用数据视图,

   DataView m_DataView;
   e.HasMorePages = false;
  for (rowCounter = currentRow; (rowCounter <= (this.DataView.Count - 1)); rowCounter++) 
  {
       float currentRowHeight = PrintDetailRow(leftMargin, currentPosition, this.MinDetailRowHeight, this.MaxDetailRowHeight, width, e, this.DataView(rowCounter), true);
       if ((currentPosition + (currentRowHeight < footerBounds.Y)))
        {
            // it will fit on the page
               currentPosition = (currentPosition + PrintDetailRow(leftMargin, currentPosition, MinDetailRowHeight, MaxDetailRowHeight, width, e, this.DataView(rowCounter), false));
        }
        else
        {
           e.HasMorePages = true;
           currentRow = rowCounter;
           break;
         }

    }


public DataView DataView {
    get {
        return m_DataView;
    }
    set {
        m_DataView = value;
    }
}

protected object GetField(DataRowView row, string fieldName) {
    object obj = null;
    if (!(m_DataView == null)) {
        obj = row(fieldName);
    }
    return obj;
}

// relevant snippet out of OnPrintPage
private int rowCounter;

请查看此链接 更多信息

我希望它会对您有所帮助......

这是 printdetailrow 函数......

   protected virtual float PrintDetailRow(float x, float y, float minHeight, float maxHeight, float width, PrintPageEventArgs e, DataRowView row, bool sizeOnly)
   {
    Graphics g = e.Graphics;
    string detailText;
    RectangleF detailTextLayout = new RectangleF(x, y, width, maxHeight);
    float detailHeight;
    Font detailRowFont;
    StringFormat detailStringFormat = new StringFormat(StringFormatFlags.LineLimit);
    detailStringFormat.Trimming = StringTrimming.EllipsisCharacter;
    ColumnInformation ci;
    int cols;
    for (cols = 0; (cols 
                <= (this.ColumnCount - 1)); cols++)
     {
        // use the provided functions, 
        // instead of going after the internal ArrayList 
        // and the CType() work is taken care of for you.
        ci = this.GetColumn(cols);
        // if a font is not specified, use the DetailFont property of the report class
        if ((ci.DetailFont == null))
        {
            detailRowFont = this.DetailFont;
        }
        else
        {
            detailRowFont = ci.DetailFont;
        }
        detailText = ci.GetString(this.GetField(row, ci.Field));
        detailStringFormat.Alignment = ci.Alignment;
        detailTextLayout.Width = ci.Width;
        if (((detailTextLayout.X - x) 
                    >= width))
        {
            // none of it will fit onto the page
            break;
        }
        else if (((detailTextLayout.X 
                    + (detailTextLayout.Width - x)) 
                    > width))
        {
            // some of it won't fit onto the page
            detailTextLayout.Width = (width 
                        - (detailTextLayout.X - x));
        }
        detailHeight = Math.Max(g.MeasureString(detailText, detailRowFont, detailTextLayout.Size, detailStringFormat, 0, 0).Height, detailHeight);
        // hmm... no space between columns?
        // This code lines the columns up flush, 
        // but if you wished to add a space between 
        // them you would need to add it to X here, 
        // in the check above to determine if the column 
        // will fit, and in the corresponding parts 
        // of the next For loop.
        detailTextLayout.X = (detailTextLayout.X + detailTextLayout.Width);
    }
    detailTextLayout.X = x;
    detailTextLayout.Height = Math.Max(Math.Min(detailHeight, maxHeight), minHeight);
    if (!sizeOnly) {
        for (cols = 0; (cols 
                    <= (this.ColumnCount - 1)); cols++) {
            ci = this.GetColumn(cols);
            if ((ci.DetailFont == null)) {
                detailRowFont = this.DetailFont;
            }
            else {
                detailRowFont = ci.DetailFont;
            }
            detailText = ci.GetString(this.GetField(row, ci.Field));
            detailStringFormat.Alignment = ci.Alignment;
            detailTextLayout.Width = ci.Width;
            if (((detailTextLayout.X - x) 
                        >= width)) {
                // none of it will fit onto the page
                break;
            }
            else if (((detailTextLayout.X 
                        + (detailTextLayout.Width - x)) 
                        > width)) {
                // some of it won't fit onto the page
                detailTextLayout.Width = (width 
                            - (detailTextLayout.X - x));
            }
            g.DrawString(detailText, detailRowFont, DetailBrush, detailTextLayout, detailStringFormat);
            detailTextLayout.X = (detailTextLayout.X + detailTextLayout.Width);
        }
        detailTextLayout.X = x;
        detailTextLayout.Y = y;
        detailTextLayout.Height = detailHeight;
        detailTextLayout.Width = width;
    }
    return detailTextLayout.Height;
}

you have to set the margins for table also , here a sample code how to set the margins with data and using dataview also

   DataView m_DataView;
   e.HasMorePages = false;
  for (rowCounter = currentRow; (rowCounter <= (this.DataView.Count - 1)); rowCounter++) 
  {
       float currentRowHeight = PrintDetailRow(leftMargin, currentPosition, this.MinDetailRowHeight, this.MaxDetailRowHeight, width, e, this.DataView(rowCounter), true);
       if ((currentPosition + (currentRowHeight < footerBounds.Y)))
        {
            // it will fit on the page
               currentPosition = (currentPosition + PrintDetailRow(leftMargin, currentPosition, MinDetailRowHeight, MaxDetailRowHeight, width, e, this.DataView(rowCounter), false));
        }
        else
        {
           e.HasMorePages = true;
           currentRow = rowCounter;
           break;
         }

    }


public DataView DataView {
    get {
        return m_DataView;
    }
    set {
        m_DataView = value;
    }
}

protected object GetField(DataRowView row, string fieldName) {
    object obj = null;
    if (!(m_DataView == null)) {
        obj = row(fieldName);
    }
    return obj;
}

// relevant snippet out of OnPrintPage
private int rowCounter;

pls take a look at this Link for more info

i hope it will helps you....

this is printdetailrow function...

   protected virtual float PrintDetailRow(float x, float y, float minHeight, float maxHeight, float width, PrintPageEventArgs e, DataRowView row, bool sizeOnly)
   {
    Graphics g = e.Graphics;
    string detailText;
    RectangleF detailTextLayout = new RectangleF(x, y, width, maxHeight);
    float detailHeight;
    Font detailRowFont;
    StringFormat detailStringFormat = new StringFormat(StringFormatFlags.LineLimit);
    detailStringFormat.Trimming = StringTrimming.EllipsisCharacter;
    ColumnInformation ci;
    int cols;
    for (cols = 0; (cols 
                <= (this.ColumnCount - 1)); cols++)
     {
        // use the provided functions, 
        // instead of going after the internal ArrayList 
        // and the CType() work is taken care of for you.
        ci = this.GetColumn(cols);
        // if a font is not specified, use the DetailFont property of the report class
        if ((ci.DetailFont == null))
        {
            detailRowFont = this.DetailFont;
        }
        else
        {
            detailRowFont = ci.DetailFont;
        }
        detailText = ci.GetString(this.GetField(row, ci.Field));
        detailStringFormat.Alignment = ci.Alignment;
        detailTextLayout.Width = ci.Width;
        if (((detailTextLayout.X - x) 
                    >= width))
        {
            // none of it will fit onto the page
            break;
        }
        else if (((detailTextLayout.X 
                    + (detailTextLayout.Width - x)) 
                    > width))
        {
            // some of it won't fit onto the page
            detailTextLayout.Width = (width 
                        - (detailTextLayout.X - x));
        }
        detailHeight = Math.Max(g.MeasureString(detailText, detailRowFont, detailTextLayout.Size, detailStringFormat, 0, 0).Height, detailHeight);
        // hmm... no space between columns?
        // This code lines the columns up flush, 
        // but if you wished to add a space between 
        // them you would need to add it to X here, 
        // in the check above to determine if the column 
        // will fit, and in the corresponding parts 
        // of the next For loop.
        detailTextLayout.X = (detailTextLayout.X + detailTextLayout.Width);
    }
    detailTextLayout.X = x;
    detailTextLayout.Height = Math.Max(Math.Min(detailHeight, maxHeight), minHeight);
    if (!sizeOnly) {
        for (cols = 0; (cols 
                    <= (this.ColumnCount - 1)); cols++) {
            ci = this.GetColumn(cols);
            if ((ci.DetailFont == null)) {
                detailRowFont = this.DetailFont;
            }
            else {
                detailRowFont = ci.DetailFont;
            }
            detailText = ci.GetString(this.GetField(row, ci.Field));
            detailStringFormat.Alignment = ci.Alignment;
            detailTextLayout.Width = ci.Width;
            if (((detailTextLayout.X - x) 
                        >= width)) {
                // none of it will fit onto the page
                break;
            }
            else if (((detailTextLayout.X 
                        + (detailTextLayout.Width - x)) 
                        > width)) {
                // some of it won't fit onto the page
                detailTextLayout.Width = (width 
                            - (detailTextLayout.X - x));
            }
            g.DrawString(detailText, detailRowFont, DetailBrush, detailTextLayout, detailStringFormat);
            detailTextLayout.X = (detailTextLayout.X + detailTextLayout.Width);
        }
        detailTextLayout.X = x;
        detailTextLayout.Y = y;
        detailTextLayout.Height = detailHeight;
        detailTextLayout.Width = width;
    }
    return detailTextLayout.Height;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文