c# - object cannot be cast from DB Null or other types -


i have datagridview in windows application.in want calculate total amount using datagridview cell(from qty,price,tax,discount) values , display in total gridview cell. getting error.i had taken nullable type also.how overcome problem. ? here cellendedit event code

 private void datagrid_cellendedit(object sender, datagridviewcelleventargs e)         {             int val1 = convert.toint32(datagrid.rows[e.rowindex].cells[2].value);             int val2 = convert.toint32(datagrid.rows[e.rowindex].cells[3].value);             int val3 = convert.toint32(datagrid.rows[e.rowindex].cells[4].value);             int val4 = convert.toint32(datagrid.rows[e.rowindex].cells[5].value);                  datagrid.rows[e.rowindex].cells[6].value = ((val2 + val3) * val1) - val4;          }  

the issue have if 1 of cells dbnull.value. check see if cell not equal dbnull.value, , convert in. if value infact true null, assume setting value 0 fine.

int value1 = datagrid.rows[e.rowindex].cells[2].value == dbnull.value ? 0 : convert.toint32(datagrid.rows[e.rowindex].cells[2].value); 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -