lundi 20 avril 2015

Specified cast is not valid error in Datagridview c#

This is a really weird error I've never had before. I have added a checkbox to my datagridview like this:

DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
datagrid.Columns.Insert(0, checkBoxColumn);

I get the error

Specified cast is not valid

And it highlights this in cell formating of the datagrid:

for (int i = 0; i <= this.datagrid.Rows.Count - 1; i++)
{
    if ((int)datagrid.Rows[e.RowIndex].Cells[7].Value <= 5)
    {
        this.datagrid.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
    }

This checks if a value in cell 7 is below the value or equal to 5 and if it is changes it to red. I have tried changing the cell to 8 because I thought it had moved since I've added a new column at 0 however it didn't work.

Aucun commentaire:

Enregistrer un commentaire