2011-10-19 19 views
0

我有一個PrintPreviewDialog和TabularReport的窗體。 我也有一個DataGridView。C#PrintPreviewDialog文檔設置爲TabularReport,但沒有顯示

我從我的數據庫填充一個DataView對象。現在沒有想到如何,但它的工作原理。 當我在DataGridView上使用DataView時,可以看到正確的行和顏色。我用這個代碼:

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

但是,當我使用相同的數據視圖我TabularReport,然後嘗試看到它在我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() 

有沒有人知道爲什麼林進入空白頁? 謝謝!

回答

0

你必須設置爲表中的利潤也,這裏的樣本代碼如何設置數據的邊緣,並使用數據視圖也

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; 

請看看這個鏈接more info

我希望它會幫助你....

這是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; 
} 
+0

我找不到PrintDetailRow函數,即使沒有下載。 –

+0

@BarakMitz看到我的編輯,即打印細節行函數..如果你實際上下載該文件這是printdetailrow函數的路徑WindowsformPrinting項目---> Tabularreport.vb ----> printdetailrow函數.... –

+0

謝謝我現在看到它...你有C#代碼嗎?這會非常有幫助。謝謝 –

相關問題