【问题标题】:DataGridView CheckBox - Print checked rowsDataGridView CheckBox - 打印选中的行
【发布时间】:2015-11-09 09:23:03
【问题描述】:

我是编程初学者。 我创建了一个 sql 查询来填充 DataGridView 并在 Column[0] 中添加了 CheckBox 列。我创建了检查事件。但我不知道如何向前迈进。我想在

  • 第一步:添加检查完成按钮和事件以仅显示选中的列。

  • 第二步: 以某种方式打印所有选定的列行单元格 1(名称)和单元格 2(ID)。

  • 第三步:我想创建一个模板,以在矩形或某些对象内将此名称和 ID 打印为 A4 格式。

因为我是新来的,所以我需要很多帮助!

提前致谢!

ps:每一步都会对我有很大帮助!

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (string.Compare(dataGridView1.CurrentCell.OwningColumn.Name, "Checked") == 0)
    {
        bool checkBoxStatus = Convert.ToBoolean(dataGridView1.CurrentCell.EditedFormattedValue);

        //"CheckBoxColumn" column value is checked or not. 
        if (checkBoxStatus)
        {
            MessageBox.Show("1");//for check it works or not
        }
        else
        {
            MessageBox.Show("0");//for check it works or not
        }
    }
}

【问题讨论】:

    标签: c# winforms printing datagridview


    【解决方案1】:

    要检测检查的行,当您的第一列是 DataGridViewCheckBoxColumn 时,您可以使用以下代码:

    var checkedRows = this.dataGridView1.Rows.Cast<DataGridViewRow>()
                          .Where(row => (bool?)row.Cells[0].Value == true)
                          .ToList();
    

    对于其余问题,您可以使用以下任一选项:

    选项1:创建RDLC Report

    如果您使用DataTable 或业务对象作为网格模型,您可以简单地创建一个 RDLC 报告并将选中的行传递给报告并打印报告。

    选项2:使用PrintDocument打印

    要打印,请使用PrintDocument 并处理PrintPage 事件并将打印逻辑和代码放在那里。要触发打印事件,只需在代码中的某处调用 printDocument1.Print() 即可。

    您可以遍历checkedRows 并使用e.Graphics.DrawString 打印每一行的值。

    例如:

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        //Find all checked rows
        var allCheckedRows = this.dataGridView1.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 concatenate values using ", " as separator, and added to output
            builder.AppendLine(string.Join(", ", cellValues));     
        });
    
        //Print the output string
        e.Graphics.DrawString(builder.ToString(),
                    this.myDataGridView.Font,
                    new SolidBrush(this.dataGridView1.ForeColor),
                    new RectangleF(0, 0, this.printDocument1.DefaultPageSettings.PrintableArea.Width, this.printDocument1.DefaultPageSettings.PrintableArea.Height));
    }
    

    【讨论】:

    • Reza Aghaei 谢谢你,它非常接近我想要的!
    • Reza Aghaei - 我可以将要放置在 A4 尺寸纸上的行定位吗?例如:第一行到 10.10 秒到 20.10 等等。
    • 是的,您可以将任何逻辑应用于绘图,我的代码只是逐行打印所有行的示例。我将所有值添加到一个字符串中,然后使用DrawString 重新定义了该字符串。您可以在 checkedRows 上使用 for 循环并调用 DrawString 传递合适的文本和矩形。
    • Reza Aghaei - 我无法成功。我觉得太难了!你能帮我写代码以了解它是如何工作的吗?
    • 你也可以查看我的回答herehere
    【解决方案2】:

    向 DataGridView 添加按钮:

    DataGridViewButtonColumn editButton = new DataGridViewButtonColumn();
    editButton.HeaderText = "Edit";
    editButton.Text = "Edit";
    editButton.UseColumnTextForButtonValue = true;
    editButton.Width = 80;
    dbgViewObj.Columns.Add(editButton);
    

    编辑: 您可以从前一个创建一个新的 DataTable 并在 for 循环中迭代以检查复选框的状态,并使用 dgv.Rows.RemoveAt(index) 方法清除未选中的行。然后使用 dgv.Columns.Columns.Remove("column_name") 清除复选框列本身。

    【讨论】:

    • Gnqz 感谢您的评论,非常有用!用于删除未选中的行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2010-10-08
    相关资源
    最近更新 更多