我是vb.net编程的新手。我发现它很难在数据网格视图中打印选定的列。有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. 如何在vb.net的datagridview中打印选定的列?
- 2. 如何在vb.net中打印datagridview结果?
- 3. 在vb.net中打印Datagridview数据
- 4. 如何打印dataGridview
- 5. 如何在C#打印选定行从DataGridView与SQL
- 6. 如何在C#中打印DataGridView?
- 7. 使用vb.net从datagridview打印到热敏打印机
- 8. 如何打印DataGridView数据?
- 9. 如何打印datagridview结果?
- 10. 如何只从c中的datagridview打印选中的行#
- 11. 如何在vb.net中打印网页
- 12. 如何在vb.net 2010中打印表单?
- 13. DataGridView列中的RadioButton列VB.NET
- 14. 在Winform C中打印DataGridView#
- 15. 如何打开vb.net中的打印队列窗口
- 16. 把自定义列表中的datagridview vb.net
- 17. 绑定列表(的类)datagridview在vb.net
- 18. vb.net/DataGridView的选择?
- 19. 如何计算vb.net中datagridview中特定列的行值总和
- 20. 如何从vb.net datagridview中的特定列中获取值?
- 21. 打印DataGridView
- 22. 打印.NET DataGridView
- 23. 打印DataGridView
- 24. 如何在vb.net列表框中打印三角形
- 25. vb.net/DataGridView/ComboBox列?
- 26. Vb.net/DataGridView/ComboBox列
- 27. 打印在VB.NET 2010
- 28. 如何打印选定的项目下拉列表中HTHML
- 29. 如何使用数据库中的选定值打印列表?
- 30. 如何打印由选项卡分隔的选定列?