[WPF] "Unable to cast object of type 'System.Data.DataRow' to type 'System.IConvertible'."

c00012 746 Reputation points
2021-07-23T04:58:06.227+00:00

Hello,
I have an amortization table of Personal Loan example:
117229-ice-screenshot-20210721-212616.png

To implement this, I wrote the following code using datatable.

        //create datatable and add column  
        Amortable = new DataTable();  
        Amortable.Columns.Add("Terms", typeof(int));  
        Amortable.Columns.Add("Principal", typeof(double));  
        Amortable.Columns.Add("Interest", typeof(double));  
        Amortable.Columns.Add("Payment", typeof(double));  
        Amortable.Columns.Add("Balance", typeof(double));  

        //insert row at first row  
        DataRow row = Amortable.NewRow();  
        row["Balance"] = Convert.ToDouble(off.LoanAmt);  
        row["Terms"] = 0;  
        row["Payment"] = 0.0;  
        row["Interest"] = 0.0;  
        row["Principal"] = 0.0;  
        Amortable.Rows.InsertAt(row, 0);  
          
        //add row   
        for (int i = 0; i <Convert.ToInt32(off.Terms) * 12; i++)  
        {  
            DataRow dataRow = Amortable.NewRow();  
            dataRow["Terms"] = i + 1;  
            dataRow["Payment"] = Convert.ToDouble(off.Payment);  
            dataRow["Interest"] = Convert.ToDouble(off.APR) / 12 / 100 * Convert.ToDouble(Amortable.DefaultView.ToTable(false,"Balance").Rows[Amortable.Rows.Count-1]); //**to get the value of previous row**  
            dataRow["Principal"] = Convert.ToDouble(dataRow["Payment"]) - Convert.ToDouble(dataRow["Interest"]);  
            dataRow["Balance"] = Convert.ToDouble(Amortable.DefaultView.ToTable(false, "Balance").Rows[Amortable.Rows.Count-1]) - Convert.ToDouble(dataRow["Principal"]);//**to get the value of previous row**  
            if (Convert.ToDouble(dataRow["Balance"]) < 0.0)  
                dataRow["Balance"] = 0.0;  
            Amortable.Rows.Add(dataRow);  
        }  

after running above code, I've got this:
117247-ice-screenshot-20210723-134840.png

I couldn't figure out what's wrong with it.
If someone pick me my mistake in this code, I would be very appreciated.

thanks,
c00012

Developer technologies Windows Presentation Foundation
0 comments No comments
{count} votes

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.