如何在C#中的DataGridView只打印选中行 [英] how to print only the checked rows from a datagridview in c#

查看:146
本文介绍了如何在C#中的DataGridView只打印选中行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它有一个checkboxcolumn一个DataGridView!当数据被加载标准
的所有行得到遏制!但我需要取消其中的一些,他们,把它的值,以一个变种,并打印!



这可能吗?



例:

  ------------------- ------------------------------------------- 
ColumnCheckBox | COLUMN1 |列2 |栏3
--------------------------------------------- -----------------
检查| 1251000014 | portraitx | U $ 125.00
检查| 1251000021 | notebooky | U $ 899.96
选中| 1265888512 | tabletx | U $ 899.96
检查| 1251444251 | iphoness | U $ 566.26
选中| 1255222142 | opticalreader | U $ $ 99.99 b $ B



我想要得到选中行的值和发送到VAR和打印它!主要是...如何将此值发送到一个变种?



thankx提前!


解决方案

打印所有选中的行中的一个页面:



 私人无效printDocument1_PrintPage(对象发件人,PrintPageEventArgs E)
{
//找到所有托运的行
VAR allCheckedRows = this.myDataGridView.Rows.Cast<&的DataGridViewRow GT;()
。凡(行= GT;(?布尔)row.Cells [0] .value的== true)而
.ToList();

//创建一个StringBuilder将包含字符串的所有行检查
变种建设者=新的StringBuilder();

//对于每一个托运的行,创建行字符串呈现,并加入到输出的StringBuilder
allCheckedRows.ForEach(行=>
{
//创建一个行的所有单元格值的数组,然后用分离器
变种将它们连接起来cellValues = row.Cells.Cast<&DataGridViewCell的GT;()
。凡(电池=> cell.ColumnIndex大于0)
。选择(电池=>的String.Format({0},cell.Value))
.ToArray();使用

//然后joiconcatenate值 作为分隔符,并添加到输出
builder.AppendLine(的string.join(,,cellValues));
//这里,而不是将它们添加到StringBuilder,您可以添加INT到另一个。名单
});

//打印输出字符串
e.Graphics.DrawString(builder.ToString(),
this.myDataGridView.Font,
新SolidBrush(this.myDataGridView .ForeColor)
新的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



打印每一个单独的页面检查行:



 私人INT currentPrintingRowIndex = 0; 

私人无效printDocument1_PrintPage(对象发件人,PrintPageEventArgs E)
{
VAR allCheckedRows = this.myDataGridView.Rows.Cast<&的DataGridViewRow GT;()
。凡(行= GT;(?布尔)row.Cells [0] .value的== true)而
.ToList();

如果(allCheckedRows.Count> currentPrintingRowIndex)
{
变种建设者=新的StringBuilder();
VAR currentCheckedRow = allCheckedRows [currentPrintingRowIndex]

变种cellValues = currentCheckedRow.Cells.Cast&所述;的DataGridViewCell>()
。凡(小区= GT; cell.ColumnIndex大于0)
。选择(小区= GT;的String.Format({0},cell.Value))
.ToArray();

builder.AppendLine(的string.join(,,cellValues));

e.Graphics.DrawString(builder.ToString(),
this.myDataGridView.Font,
新SolidBrush(this.myDataGridView.ForeColor),
新的RectangleF (0,0,this.printDocument1.DefaultPageSettings.PrintableArea.Width,this.printDocument1.DefaultPageSettings.PrintableArea.Height));

currentPrintingRowIndex + = 1;
e.HasMorePages = allCheckedRows.Count> currentPrintingRowIndex;
}
}



输出:



3页的文档:

 第1页的内容:1251000014,portraitx, U $ 125.00 
第2页内容:1251000021,notebooky,U $ 899.96
第3页内容:1251444251,iphoness,U $ 566.26


I have a Datagridview it has a checkboxcolumn! when the data is loaded for standard all rows get checked! but i need to uncheck some of them and them, send it's values to a var and print it!

is it possible?

Exemple:

--------------------------------------------------------------
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

I want to get the values of the CHECKED Rows and send to a var and the print it! the main is ...how to send this values to a var?

thankx in advance!

解决方案

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));
}

Output:

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;
    }
}

Output:

A document with 3 pages:

Page1 content: 1251000014, portraitx, U$ 125.00
Page2 content: 1251000021, notebooky, U$ 899.96
Page3 content: 1251444251, iphoness, U$ 566.26

这篇关于如何在C#中的DataGridView只打印选中行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆