sudhir kumar

sudhir kumar

  • NA
  • 19
  • 63.6k

Calculation in Data Grid

Apr 17 2011 1:11 AM
//  Please solve this problem  Object cannot be cast from DBNull to other types.

  private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 || e.ColumnIndex == 1)
            {
                int cell1 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
                int cell2 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
                if (cell1.ToString() != "" && cell2.ToString() != "")
                {
                    dataGridView1.CurrentRow.Cells[2].Value = cell1 + cell2;
                }
            }
        }
// I am doing this. but it is showing below error...

Object cannot be cast from DBNull to other types.

so please try to solve.

Attachment: data grid.zip

Answers (6)