Exception when using T Field <T> in my code

I get an InvalidCastException when I try to use this code. I can not decide how to use the field. Plz help me

DataTable _Transaction= new DataTable();
float NetAmount=0;
//Records inserted into Table 

  for (int i = 0; i < _Transaction.Rows.Count; i++)
            {
                NetAmount += _Transaction.Rows[i].Field<object>("ItemAmount");
            }

EDIT I really want to get the value in the specified column Name and Row no from dataTable. _Transaction is the name of the table here

+3
source share
1 answer

You will need to convert _Transaction.Rows[i].Field<object>("ItemAmount")to the same type as NetAmountfor the operator to +=work on the same type.

, NetAmount decimal, Field<decimal> Field<object>. , Convert.ToDecimal .

+2

Source: https://habr.com/ru/post/1728989/


All Articles