Hello C# experts!

can't figure it out. obviously i'm missing something.
here is part of my code:

private void button1_Click(object sender, EventArgs e)


            {
                dataGridView1.ColumnCount = 5;
                dataGridView1.Columns[0].HeaderText = "Payment #";
                dataGridView1.Columns[1].HeaderText = "Monthly Payment";
                dataGridView1.Columns[2].HeaderText = "Amount to Interest";
                dataGridView1.Columns[3].HeaderText = "Amount to Principal";
                dataGridView1.Columns[4].HeaderText = "Remaining Balance";

                double loan, cost, downPmt, months, APR = 0, newAPR, usedAPR, monthlyPmt = 0, interest = 0, principal = 0;

                cost = double.Parse(textBox1.Text);
                downPmt = double.Parse(textBox2.Text);
                months = double.Parse(textBox3.Text);
                loan = cost - downPmt;

                newAPR = .069;
                usedAPR = .085;

                dataGridView1.Rows.Clear();
                for (int payments = 1; payments <= months; payments++)

                {

                    if (radioButton1.Checked)
                        textBox4.Text = newAPR.ToString("0.0%");
                    else
                        textBox4.Text = usedAPR.ToString("0.0%");
                    if (radioButton1.Checked)
                        APR = newAPR;
                    else
                        APR = usedAPR;



                    monthlyPmt = Financial.Pmt(APR, months, -loan);
                    principal = Financial.PPmt(APR, months, months, -loan);
                    interest = Financial.IPmt(APR, months, months, -loan);



                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[payments - 1].Cells[0].Value = payments;
                    dataGridView1.Rows[payments - 1].Cells[1].Value = monthlyPmt.ToString("c");
                    dataGridView1.Rows[payments - 1].Cells[2].Value = interest.ToString("c");
                    dataGridView1.Rows[payments - 1].Cells[3].Value = principal.ToString("c");



                    loan -= principal;
                    dataGridView1.Rows[payments - 1].Cells[4].Value = loan.ToString("c");
                    interest += principal;


                }

        }

and it's coming out like this, which is incorrect.
http://oi50.tinypic.com/faszo6.jpg

it's supposed to be like this:
http://oi45.tinypic.com/1zoje6g.jpg

i must be doing something wrong in the interest calculations?

i'd appreciate the help, thanks.

The obvious thing I can think off is there is something wrong in the calculations in your Financial class. The code you send us just fills in a DataGridView.

You should calculate the monthly payment once based on the initial loan amount and not in your loop based on remaining balance.

Are you using the Financial Class from the VisualBasic Namespace? If so, then your usage is incorrect and you should review the documentation.

commented: Great answer +14

@TnTinMN, thanks for the info, did not even know such a thing existed.

@ddanbe: You are welcome. :) VB has a few utility classes that they really should publicize better to the .Net world or better yet, move to generic Utility namespace.

i finally got it! so stoked!

i didn't need the fancy PPmt and IPmt. just a simple formula.

//principal = Financial.PPmt(APR/12, months, months, -loan);
//interest = Financial.IPmt(APR / 12, months, months, -loan);

                    interest = APR / 12 * loan;
                    principal = monthlyPmt - interest;

Then I had to move the monthlyPmt and if-else above the loop.

yup, i was using Microsoft.VisualBasic namespace for the Financial.Pmt class.

Thanks TnTinMN.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.