how to print only the checked rows from a datagridview in c#

后端 未结 2 1914
时光取名叫无心
时光取名叫无心 2021-01-27 16:51

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

2条回答
  •  一生所求
    2021-01-27 16:59

    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()
                                    .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()
                .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()
                                    .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()
                    .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
    

提交回复
热议问题