c#从dataGridView打印已检查行的单元格

时间:2015-11-12 12:45:26

标签: c# checkbox printing datagridview

enter image description here我有一张这样的表。

  CheckBoxColumn  Name   ID
 --------------------------
  false            John   01
  true             Peter  02
  true             Steve  03

我想只打印分隔的标记细胞行。

结果必须是这样的:

Peter 

                   02
Steve

                   03

复选框列添加“编辑datagridView选项”。

我在这里找到了类似的解决方案,但不是这样的。我用它但效果不好。我想请求帮助以获得正确的代码。 我的代码:

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


            e.Graphics.DrawString(dataGridView1.Rows[1].Cells[1].FormattedValue.ToString(),this.dataGridView1.Font, new SolidBrush(this.dataGridView1.ForeColor), new Point(0, 10)); 
            e.Graphics.DrawString(dataGridView1.Rows[1].Cells[2].FormattedValue.ToString(),this.dataGridView1.Font, new SolidBrush(this.dataGridView1.ForeColor), new Point(20, 200)); ;

            e.Graphics.DrawString(dataGridView1.Rows[2].Cells[1].FormattedValue.ToString(), this.dataGridView1.Font, new SolidBrush(this.dataGridView1.ForeColor), new Point(0, 30));
            e.Graphics.DrawString(dataGridView1.Rows[2].Cells[2].FormattedValue.ToString(), this.dataGridView1.Font, new SolidBrush(this.dataGridView1.ForeColor), new Point(20, 230)); ;    
         }

现在,结果只给出了第一行和第二行的所有时间,而不是已检查的行。

有人能帮助我得到正确的结果吗?

提前Thx!

我认为它可以是“已检查状态”检查。

结果必须是!

1 个答案:

答案 0 :(得分:1)

您正在重复for(const auto& d : data) { [...] } ,但在allCheckedRows内调用rowforeach(第二行和第三行)而不是使用dataGridView1.Rows[1] )。 此外,您应该有一个变量来增加绘制的高度,这样您就不会将所有记录绘制在彼此之上。 在您的代码中,您为每个选中的列反复绘制第二行和第三行。

这是一个可能的解决方案:

dataGridView1.Rows[2]