jeudi 19 novembre 2015

c# printing checked rows but gives 'System.InvalidCastException'

Here is my code. It worked well but something happend and i could not find the problem:

The problem is 'System.InvalidCastException' And it comes in line

 'if (checkedRow == null || (bool)checkedRow.Cells[0].Value != true)  continue; //Skip the row if it's not checked '

What can cause this?

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {


        int height = 0;
        int height1 = 0;
        int rows =0  ;


         foreach (var row in dataGridView1.Rows)
         {
             DataGridViewRow checkedRow = row as DataGridViewRow;
             if (checkedRow == null || (bool)checkedRow.Cells[0].Value != true)

                 continue; //Skip the row if it's not checked 

              for (i = 0; i <= dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Megjelöl"].Value) == true)
                {

                    ++rows;
                }
            }


                 if (height >= 800)
                 {

                 }

                 if (height <= 800)
                 {


                 }

         }
    }




Aucun commentaire:

Enregistrer un commentaire