C#对象无法从dbnull转换为其他类型

时间:2015-08-12 07:46:29

标签: c# object datagridview casting dbnull

我在datagridview CellFormatting事件中收到此异常。 我在这个事件中所做的就是根据datagridview单元格值改变单元格样式。如何在此类操作中出现此错误?谁能帮我? 事件方法如下:

private void datagridPM_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        try
        {
            if (datagridPM.Rows[e.RowIndex].Cells[datagridPM.ColumnCount - 1].Value.ToString() == "RUNNING")
            {
                datagridPM.Rows[e.RowIndex].Cells[datagridPM.ColumnCount - 1].Style = new DataGridViewCellStyle { ForeColor = Color.Green, BackColor = Color.White };
                if (e.RowIndex == current_selected.RowIndex)
                    this.Invoke(new Action(() => strt_btn.Text = "STOP"));


            }
            else if (datagridPM.Rows[e.RowIndex].Cells[datagridPM.ColumnCount - 1].Value.ToString() == "STOPPED")
            {
                datagridPM.Rows[e.RowIndex].Cells[datagridPM.ColumnCount - 1].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.White };
                if (e.RowIndex == current_selected.RowIndex)
                    this.Invoke(new Action(() => strt_btn.Text = "START"));
            }               

            datagridPM.Columns[2].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
            datagridPM.Columns[datagridPM.ColumnCount - 1].DefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);

            if (Convert.ToDouble(datagridPM.Rows[e.RowIndex].Cells[datagridPM.ColumnCount - 6].Value)>80.00)
            {

                datagridPM.Rows[e.RowIndex].Cells[datagridPM.ColumnCount - 6].Style = new DataGridViewCellStyle { ForeColor = Color.Red, BackColor = Color.Wheat };

            }
            else
            {
                datagridPM.Rows[e.RowIndex].Cells[datagridPM.ColumnCount - 6].Style = new DataGridViewCellStyle { ForeColor = Color.Black, BackColor = Color.White };

            }

        }
        catch (Exception ex)
        {

            MDIParent.thisMdiObj.Invoke(new Action(() => MDIParent.thisMdiObj.txtLog.AppendText(DateTime.Now.ToString() + " : " + "CELL FORMATTING EXCEPTION : " + ex.Message.ToString() + "::" + ex.StackTrace + Environment.NewLine)));
        }
    }

0 个答案:

没有答案