2,854 questions
I solved problem myself. I found this in stackoverflow.com and applied to it.
https://stackoverflow.com/questions/13816490/get-cell-value-from-a-datatable-in-c-sharp
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I have an amortization table of Personal Loan example:
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:
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
I solved problem myself. I found this in stackoverflow.com and applied to it.
https://stackoverflow.com/questions/13816490/get-cell-value-from-a-datatable-in-c-sharp