私はvb.netプログラミングの初心者です。私はそれが本当に難しいのdatagridviewで選択された列を印刷しているのを見ている。 12の列があり、私はランドスケープビューでほんの少しだけ印刷したいと思います。誰かが関連記事や記事を手助けできますか?vb.netで選択したDatagridview列を印刷するには?
-1
A
答えて
1
としては答えhere
コード:
private static void PrintDoc_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
int tmpWidth, i;
int tmpTop = e.MarginBounds.Top;
int tmpLeft = e.MarginBounds.Left;
int HeaderHeight=0;
try
{
// Before starting first page, it saves
// Width & Height of Headers and CoulmnType
if (PageNo == 1)
{
foreach (DataGridViewColumn GridCol in dgv.Columns)
{
if (!GridCol.Visible) continue;
// Skip if the current column not selected
if (!PrintDGV.SelectedColumns.Contains(
GridCol.HeaderText)) continue;
// Detemining whether the columns
// are fitted to the page or not.
if (FitToPageWidth)
tmpWidth = (int)(Math.Floor((double)(
(double)GridCol.Width/
(double)TotalWidth * (double)TotalWidth *
((double)e.MarginBounds.Width/
(double)TotalWidth))));
else
tmpWidth = GridCol.Width;
HeaderHeight =
(int)(e.Graphics.MeasureString(GridCol.HeaderText,
GridCol.InheritedStyle.Font, tmpWidth).Height) + 11;
// Save width & height of headres and ColumnType
ColumnLefts.Add(tmpLeft);
ColumnWidths.Add(tmpWidth);
ColumnTypes.Add(GridCol.GetType());
tmpLeft += tmpWidth;
}
}
// Printing Current Page, Row by Row
while (RowPos <= dgv.Rows.Count - 1)
{
DataGridViewRow GridRow = dgv.Rows[RowPos];
if (GridRow.IsNewRow || (!PrintAllRows && !GridRow.Selected))
{
RowPos++;
continue;
}
CellHeight = GridRow.Height;
if (tmpTop + CellHeight >=
e.MarginBounds.Height + e.MarginBounds.Top)
{
DrawFooter(e, RowsPerPage);
NewPage = true;
PageNo++;
e.HasMorePages = true;
return;
}
else
{
if (NewPage)
{
// Draw Print Title
e.Graphics.DrawString(PrintTitle,
new Font(dgv.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left,
e.MarginBounds.Top -
e.Graphics.MeasureString(PrintTitle,
new Font(dgv.Font,
FontStyle.Bold),
e.MarginBounds.Width).Height - 13);
String s = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToShortTimeString();
// Draw Time and Date
e.Graphics.DrawString(s,
new Font(dgv.Font, FontStyle.Bold),
Brushes.Black, e.MarginBounds.Left +
(e.MarginBounds.Width -
e.Graphics.MeasureString(s, new Font(dgv.Font,
FontStyle.Bold), e.MarginBounds.Width).Width),
e.MarginBounds.Top -
e.Graphics.MeasureString(PrintTitle,
new Font(new Font(dgv.Font,
FontStyle.Bold), FontStyle.Bold),
e.MarginBounds.Width).Height - 13);
// Draw Headers
tmpTop = e.MarginBounds.Top;
i = 0;
foreach (DataGridViewColumn GridCol in dgv.Columns)
{
if (!GridCol.Visible) continue;
if (!PrintDGV.SelectedColumns.Contains(
GridCol.HeaderText))
continue;
e.Graphics.FillRectangle(new
SolidBrush(Color.LightGray),
new Rectangle((int) ColumnLefts[i], tmpTop,
(int)ColumnWidths[i], HeaderHeight));
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int) ColumnLefts[i], tmpTop,
(int)ColumnWidths[i], HeaderHeight));
e.Graphics.DrawString(GridCol.HeaderText,
GridCol.InheritedStyle.Font,
new SolidBrush(GridCol.InheritedStyle.ForeColor),
new RectangleF((int)ColumnLefts[i], tmpTop,
(int)ColumnWidths[i], HeaderHeight), StrFormat);
i++;
}
NewPage = false;
tmpTop += HeaderHeight;
}
// Draw Columns Contents
i = 0;
foreach (DataGridViewCell Cel in GridRow.Cells)
{
if (!Cel.OwningColumn.Visible) continue;
if (!SelectedColumns.Contains(
Cel.OwningColumn.HeaderText))
continue;
// For the TextBox Column
if (((Type) ColumnTypes[i]).Name ==
"DataGridViewTextBoxColumn" ||
((Type) ColumnTypes[i]).Name ==
"DataGridViewLinkColumn")
{
e.Graphics.DrawString(Cel.Value.ToString(),
Cel.InheritedStyle.Font,
new SolidBrush(Cel.InheritedStyle.ForeColor),
new RectangleF((int)ColumnLefts[i],
(float)tmpTop,
(int)ColumnWidths[i],
(float)CellHeight), StrFormat);
}
// For the Button Column
else if (((Type) ColumnTypes[i]).Name ==
"DataGridViewButtonColumn")
{
CellButton.Text = Cel.Value.ToString();
CellButton.Size = new Size((int)ColumnWidths[i],
CellHeight);
Bitmap bmp = new Bitmap(CellButton.Width,
CellButton.Height);
CellButton.DrawToBitmap(bmp, new Rectangle(0, 0,
bmp.Width, bmp.Height));
e.Graphics.DrawImage(bmp,
new Point((int)ColumnLefts[i], tmpTop));
}
// For the CheckBox Column
else if (((Type) ColumnTypes[i]).Name ==
"DataGridViewCheckBoxColumn")
{
CellCheckBox.Size = new Size(14, 14);
CellCheckBox.Checked = (bool)Cel.Value;
Bitmap bmp = new Bitmap((int)ColumnWidths[i],
CellHeight);
Graphics tmpGraphics = Graphics.FromImage(bmp);
tmpGraphics.FillRectangle(Brushes.White,
new Rectangle(0, 0,
bmp.Width, bmp.Height));
CellCheckBox.DrawToBitmap(bmp,
new Rectangle((int)((bmp.Width -
CellCheckBox.Width)/2),
(int)((bmp.Height - CellCheckBox.Height)/2),
CellCheckBox.Width, CellCheckBox.Height));
e.Graphics.DrawImage(bmp,
new Point((int)ColumnLefts[i], tmpTop));
}
// For the ComboBox Column
else if (((Type) ColumnTypes[i]).Name ==
"DataGridViewComboBoxColumn")
{
CellComboBox.Size = new Size((int)ColumnWidths[i],
CellHeight);
Bitmap bmp = new Bitmap(CellComboBox.Width,
CellComboBox.Height);
CellComboBox.DrawToBitmap(bmp, new Rectangle(0, 0,
bmp.Width, bmp.Height));
e.Graphics.DrawImage(bmp,
new Point((int)ColumnLefts[i], tmpTop));
e.Graphics.DrawString(Cel.Value.ToString(),
Cel.InheritedStyle.Font,
new SolidBrush(Cel.InheritedStyle.ForeColor),
new RectangleF((int)ColumnLefts[i] + 1,
tmpTop, (int)ColumnWidths[i]
- 16, CellHeight), StrFormatComboBox);
}
// For the Image Column
else if (((Type) ColumnTypes[i]).Name ==
"DataGridViewImageColumn")
{
Rectangle CelSize = new Rectangle((int)ColumnLefts[i],
tmpTop, (int)ColumnWidths[i], CellHeight);
Size ImgSize = ((Image)(Cel.FormattedValue)).Size;
e.Graphics.DrawImage((Image)Cel.FormattedValue,
new Rectangle((int)ColumnLefts[i] +
(int)((CelSize.Width - ImgSize.Width)/2),
tmpTop + (int)((CelSize.Height -
ImgSize.Height)/2),
((Image)(Cel.FormattedValue)).Width,
((Image)(Cel.FormattedValue)).Height));
}
// Drawing Cells Borders
e.Graphics.DrawRectangle(Pens.Black,
new Rectangle((int)ColumnLefts[i],
tmpTop, (int)ColumnWidths[i], CellHeight));
i++;
}
tmpTop += CellHeight;
}
RowPos++;
// For the first page it calculates Rows per Page
if (PageNo == 1) RowsPerPage++;
}
if (RowsPerPage == 0) return;
// Write Footer (Page Number)
DrawFooter(e, RowsPerPage);
e.HasMorePages = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
はまた、以下のリンク参照してください。
http://www.codeproject.com/Articles/16601/Printing-Selected-Columns-and-Rows-in-a-DataGrid
http://www.c-sharpcorner.com/forums/datagridview-printing-by-user-selecting-rows-and-columns
http://www.vbforums.com/showthread.php?523396-RESOLVED-Printing-a-Datagridview-Rows
関連する問題
- 1. Tkxでチェックボタンで選択した値を印刷するには
- 2. Elmで選択したオプションのインデックスを印刷するには?
- 3. 選択したチャートを印刷
- 4. 選択したインデックス項目を印刷
- 5. 印刷選択値
- 6. エクスポート角度データテーブルに(印刷、PDF、エクセル、コピー)選択した列
- 7. DataGridviewの印刷
- 8. R Shiny:選択した列のテーブルを印刷
- 9. GridViewで選択した行を印刷する
- 10. 選択した領域を1ページに印刷するPDF
- 11. プリンタ選択ダイアログなしで印刷
- 12. DataGridViewで選択したセルスタイル
- 13. プログラムですべてを選択した後にdatagridviewの選択
- 14. DatagridviewでRowを選択するとチェックボックスの列がチェックされますVB.NET
- 15. vb.netを使用したPDFの印刷
- 16. ページ上の選択したアイテムのみを印刷する
- 17. PHPとMySQLでselectオプションで選択した属性を印刷
- 18. jqueryプラグインから選択したオプションを印刷します
- 19. Codeigniterのドロップダウンで選択した値を印刷
- 20. グラフィックオブジェクトの印刷VB.NET
- 21. AutoHotKeyでプリンタを選択して印刷する方法
- 22. 印刷内容だけのユーザーが、ファイル - 選択>印刷
- 23. jqueryを使用して複数選択ドロップダウンリストから選択した値を印刷する方法は?
- 24. VB.NETで印刷した後に、VB.NETの画像を削除できない(KeepAutomation)
- 25. vb.netで印刷した後にボタンを無効にする方法は?
- 26. vb.netで選択したフォルダのパスを取得するには?
- 27. VB.NET PrintDialogファイルを印刷する
- 28. 複数のテーブルからデータを選択して印刷する
- 29. HTMLページの選択部分を印刷する方法は?
- 30. WinFormアプリケーションでDataGridViewをソートするときに選択した行