0

enter image description hereI have a Table like this.

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

I would like to print only the marked Cells of Rows separated.

The result have to be like this:

Peter 

                   02
Steve

                   03

The checkbox column is add with "Edit datagridView option" .

I find here an similar solution but not like this. I used it but not works well. I would like to ask for help to have a correct code. My code:

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

Now the result gives all time only the the first and second row not the checked rows.

Could anyone help me to get the correct result?

Thx in Advance!

I think it can be something with the "checked status" check.

The result have to be!

Community
  • 1
  • 1
Zoltan
  • 57
  • 10

1 Answers1

1

You're iterating allCheckedRows but then instead of using row inside your foreach you're calling dataGridView1.Rows[1] and dataGridView1.Rows[2] (the second and third row). Also, you should have a variable to increment the height in which you draw so that you won't draw all your records on top of each other. In your code you draw the second and third row on top of each other again and again for each checked column.

Here's a possible solution:

            int height = 0;
            foreach (var row in dataGridView1.Rows)
            {
                DataGridViewRow checkedRow = row as DataGridViewRow;
                if (checkedRow == null || (bool)checkedRow.Cells[0].Value == false) continue; //Skip the row if it's not checked
                height += 10;
                e.Graphics.DrawString(checkedRow.Cells[1].FormattedValue.ToString(), this.dataGridView1.Font, new SolidBrush(this.dataGridView1.ForeColor), new Point(0, height));

                height += 190;
                e.Graphics.DrawString(checkedRow.Cells[2].FormattedValue.ToString(), this.dataGridView1.Font, new SolidBrush(this.dataGridView1.ForeColor), new Point(20, height));
            }
tzachs
  • 4,331
  • 1
  • 28
  • 28
  • If you have null exception then debug and see where the null is coming from. – tzachs Nov 16 '15 at 15:01
  • http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – tzachs Nov 16 '15 at 15:01
  • 1
    So change that line to `if (checkedRow == null || (bool?)checkedRow.Cells[0].Value != true) continue; //Skip the row if it's not checked` and check if it works. – tzachs Nov 17 '15 at 21:20
  • I don't know what happened but now thows 'System.InvalidCastException'. i dont understand it. It worked me fine! – Zoltan Nov 19 '15 at 12:54
  • See which line the exception is coming from. If it's from this code then the only line which has casting is the `(bool?)checkedRow.Cells[0].Value` which means you probably have a cell in your checkbox column which is not a boolean. So either change your input or change the code to check the type of the cell. – tzachs Nov 19 '15 at 15:24