如何在c#中只打印datagridview中的已检查行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在c#中只打印datagridview中的已检查行相关的知识,希望对你有一定的参考价值。
我有一个Datagridview它有一个checkboxcolumn!当数据加载为标准时,所有行都会被检查!但我需要取消选中其中一些和它们,将它的值发送到var并打印出来!
可能吗?
例如:
--------------------------------------------------------------
ColumnCheckBox | Column1 | Column2 | Column3
--------------------------------------------------------------
checked | 1251000014 | portraitx | U$ 125.00
checked | 1251000021 | notebooky | U$ 899.96
unchecked | 1265888512 | tabletx | U$ 899.96
checked | 1251444251 | iphoness | U$ 566.26
unchecked | 1255222142 | opticalreader | U$ 99.99
我想获取CHECKED行的值并发送到var并打印它!主要是...如何将此值发送到var?
提前致谢!
答案
Print all checked rows in a page:
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
//Find all checked rows
var allCheckedRows = this.myDataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells[0].Value == true)
.ToList();
//create a stringBuilder that will contain the string for all checked rows
var builder = new StringBuilder();
//For each checked row, create string presentation of row and add to output stringBuilder
allCheckedRows.ForEach(row =>
{
//Create an array of all cell value of a row to then concatenate them using a separator
var cellValues = row.Cells.Cast<DataGridViewCell>()
.Where(cell => cell.ColumnIndex > 0)
.Select(cell => string.Format("{0}", cell.Value))
.ToArray();
//Then joiconcatenate values using ", " as separator, and added to output
builder.AppendLine(string.Join(", ", cellValues));
//Here instead of adding them to the stringBuilder, you can add int to another list.
});
//Print the output string
e.Graphics.DrawString(builder.ToString(),
this.myDataGridView.Font,
new SolidBrush(this.myDataGridView.ForeColor),
new RectangleF(0, 0, this.printDocument1.DefaultPageSettings.PrintableArea.Width, this.printDocument1.DefaultPageSettings.PrintableArea.Height));
}
输出:
1251000014, portraitx, U$ 125.00
1251000021, notebooky, U$ 899.96
1251444251, iphoness, U$ 566.26
Print each checked row in a separate page:
private int currentPrintingRowIndex = 0;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
var allCheckedRows = this.myDataGridView.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells[0].Value == true)
.ToList();
if (allCheckedRows.Count > currentPrintingRowIndex)
{
var builder = new StringBuilder();
var currentCheckedRow = allCheckedRows[currentPrintingRowIndex];
var cellValues = currentCheckedRow.Cells.Cast<DataGridViewCell>()
.Where(cell => cell.ColumnIndex > 0)
.Select(cell => string.Format("{0}", cell.Value))
.ToArray();
builder.AppendLine(string.Join(", ", cellValues));
e.Graphics.DrawString(builder.ToString(),
this.myDataGridView.Font,
new SolidBrush(this.myDataGridView.ForeColor),
new RectangleF(0, 0, this.printDocument1.DefaultPageSettings.PrintableArea.Width, this.printDocument1.DefaultPageSettings.PrintableArea.Height));
currentPrintingRowIndex += 1;
e.HasMorePages = allCheckedRows.Count > currentPrintingRowIndex;
}
}
输出:
一份3页的文件:
Page1 content: 1251000014, portraitx, U$ 125.00
Page2 content: 1251000021, notebooky, U$ 899.96
Page3 content: 1251444251, iphoness, U$ 566.26
另一答案
****这是有关Reza Aghaei的有用代码的最终法典****
private int currentPrintingRowIndex = 0;
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Font fsystem = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Pixel);
Font fdatabd = new Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel);
Font fdatabdstrikeout = new Font("Arial", 18, FontStyle.Strikeout, GraphicsUnit.Pixel);
Font fdiag = new Font("Arial", 8, FontStyle.Regular, GraphicsUnit.Pixel);
Font fbarra = new Font("C39HrP24DhTt", 30, FontStyle.Regular, GraphicsUnit.Pixel);
Font fdesc = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Pixel);
var allCheckedRows = this.dgv1.Rows.Cast<DataGridViewRow>()
.Where(row => (bool?)row.Cells[0].Value == true)
.ToList();
if (allCheckedRows.Count > currentPrintingRowIndex)
{
var builder = new StringBuilder();
var currentCheckedRow = allCheckedRows[currentPrintingRowIndex];
var cellValues = currentCheckedRow.Cells.Cast<DataGridViewCell>()
.Where(cell => cell.ColumnIndex > 0)
.Select(cell => string.Format("{0}", cell.Value))
.ToArray();
builder.Append(string.Join(",", cellValues));
// begin of the aditional implementation
string ldp = builder.ToString();
string[] cadaum = ldp.Split(',');
var cents = cadaum[3].Substring(0, 2);
var descr = cadaum[1].ToString();
if (descr.Length >= 30)
{
var descr2 = descr.Substring(0, 30);
var descr3 = descr.Substring(30);
// end of the aditional implementation
// begin modification
//UPPER LEFT SIDE
e.Graphics.DrawString("TRADEMARK", fsystem, Brushes.Black, 45, 8);
e.Graphics.DrawString("COMPANY'S NAME", fdatabd, Brushes.Black, 10, 30);
e.Graphics.DrawString("ANY OTHER INFORMATION", fdatabd, Brushes.Black, 10, 41);
e.Graphics.DrawString("made in china", fdatabd, Brushes.Black, 10, 53);
if (cadaum[1].Length >= 30)
{
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 10, 77);
}
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 10, 87);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 8, 105);
// UPPER RIGHT SIDE
e.Graphics.DrawString("TRADEMARK", fsystem, Brushes.Black, 205, 8);
e.Graphics.DrawString("COMPANY'S NAME", fdatabd, Brushes.Black, 170, 30);
e.Graphics.DrawString("ANY OTHER INFORMATION", fdatabd, Brushes.Black, 170, 41);
e.Graphics.DrawString("made in china", fdatabd, Brushes.Black, 170, 53);
if (cadaum[1].Length >= 30)
{
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 170, 77); }
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 170, 87);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 168, 105);
// CUTTING REFERENCE
e.Graphics.DrawString("---------------------------------------------------------------------------------------------", fdatabd, Brushes.Black, 1, 147);
//LOWER LEFT SIDE
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 10, 155);
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 10, 165);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 8, 177);
e.Graphics.DrawString("Price:", fdatabd, Brushes.Black, 10, 208);
e.Graphics.DrawString(cadaum[2].ToString() +"," + cents.ToString(), fdatabd, Brushes.Black, 80, 208);
e.Graphics.DrawString("TRADEMARK", fsystem, Brushes.Black, 45, 220);
// LOWER RIGHT SIDE
e.Graphics.DrawString(descr2, fdatabd, Brushes.Black, 170, 155);
e.Graphics.DrawString(descr3, fdatabd, Brushes.Black, 170, 165);
e.Graphics.DrawString("*" + cadaum[0].ToString() + "*", fbarra, Brushes.Black, 168, 177);
e.Graphics.DrawString("Price:", fdatabd, Brushes.Black, 170, 208);
e.Graphics.DrawS以上是关于如何在c#中只打印datagridview中的已检查行的主要内容,如果未能解决你的问题,请参考以下文章
C# 需要将 sql 中的数据放入 DataGridView 并在行中按日期排序
如何在C#中 双击datagridview1中的一行数据,添加到新的datagridview2中。 B/C VS2010