jeudi 12 novembre 2015

c# printing from dataGridView Cells of checked Rows

I 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 is if i check an checkbox then it 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!




Aucun commentaire:

Enregistrer un commentaire