// 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.
Loading

Wil Youmans25 reputationPosted Apr 16, 2011, 9:47 PM EDT
sudhir kumar15 reputationPosted Apr 16, 2011, 10:28 PM EDT
Sam HobbsRank #108 · 20.5K reputationPosted Apr 17, 2011, 9:03 AM EDT
I suggest that you add code to ensure that items are not null. I am not sure what you need to check for nulls but after debugging you will know what to check to at least solve that problem. I also suggest using Int32.TryParse to convert the numbers instead of Convert.ToInt32.
Wil Youmans25 reputationPosted Apr 17, 2011, 3:04 PM EDT
Sam HobbsRank #108 · 20.5K reputationPosted Apr 17, 2011, 4:30 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Apr 18, 2011, 10:40 AM EDT
int cell1 = 0;
if (!(IsDbNull(dataGridView1.CurrentRow.Cells[0].Value))
{
cell1 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
}