6
Answers

Calculation in Data Grid

sudhir kumar

sudhir kumar

13y
1.6k
1
//  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)