Need DataGridView help.

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

56 Replies - 8733 Views - Last Post: 17 January 2012 - 02:18 AM Rate Topic: -----

#1 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Need DataGridView help.

Posted 02 January 2012 - 01:34 PM

I am new to .net and I am sure this is an easy fix. I have beating my head aagainst many walls for a few weeks trying to figure it out. Any help would be greatful.

The error I am getting is:

Error 1 Value of type 'Double' cannot be converted to 'System.Windows.Forms.DataGridViewTextBoxColumn'. C:\Users\Finley & Angelique\Desktop\Visual studio 2008\Mortgage Calculator CR 17\Mortgage Calcultor CR17\Form1.vb 89 22 Mortgage Calcultor CR17



Public Class MortgageCaluclator17
    'Write the program in VB.Net (not Web based) and have it accept user input of the amount, term and interest rate. 
    'Display the mortgage payment amount. Then, list the loan balance and interest paid for each payment over the term 
    'of the loan. The list will be longer than the screen, so use loops to display a partial list, hesitate, and then 
    'display more of the list. Insert comments to document the program.


    Private Sub LoanAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoanAmountText.TextChanged

    End Sub

    Private Sub Years_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YearsText.TextChanged

    End Sub

    Private Sub InterestRate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InterestRateText.TextChanged

    End Sub

    Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        'Length math and input codes will go in thins section.

        'Setting Objects as doubles

        Dim LoanAmt As Double
        Dim Term As Integer
        Dim Payment As Double
        Dim IntRate As Decimal
        Dim Principal As Double
        Dim PaymentPeriods As Integer

        'validate loan amount input

        If IsNumeric(LoanAmountText.Text) = False Then
            MsgBox("Ok stupid, how many loan amounts, interest rates or term in years have a letter in it? Just use numbers! Please enter a valid amount.")
            'Loan amount must be numeric. Please enter a valid loan amount.
            LoanAmountText.Clear()
            YearsText.Clear()
            InterestRateText.Clear()
            LoanAmountText.Focus()
            YearsText.Focus()
            InterestRateText.Focus()

            Exit Sub

        End If

        'Setting where to get amounts
        LoanAmt = LoanAmountText.Text
        Term = YearsText.Text
        IntRate = InterestRateText.Text

        'calulate total payment periods

        PaymentPeriods = Term * 12

        'Code for the math

        Term = (12 * Term)
        IntRate = (IntRate / 100 / 12)
        Payment = (LoanAmt * IntRate) / (1 - Math.Pow(1 / (1 + IntRate), Term))

        'Sending total monthly payment to Monthly payment label

        PaymentLabel.Text = "" & FormatCurrency(Payment)

        'Sending loan information to the DataGridView


        'Creating a datagridview
        Dim i As Integer
        For i = 1 To Payment

        Next

        Dim dgvLoanDetails As New DataGridView

        'Set Number of Payments

        Dim Payments As Double
        Payments = Term

        'set interest

        IntRate = (LoanAmt * IntRate) / 12

        'set loan amount
'Here is where my error is.
        
        LoanAmount = (LoanAmt - Payment) + IntRate

        'set principle

        Principal = Payment - IntRate

        'clear datagrid view control (removes all rows)

        dgvLoanDetails.Rows.Clear()

        'calculate monthly payment and return value

        'Payment = (LoanAmt * Math.Pow((IntRate / 12) + 1, (PaymentPeriods)) * IntRate / 12) / (Math.Pow(IntRate / 12 + 1, (PaymentPeriods)) - 1)

        'making loop for the DataGridView
        If PaymentPeriods > 1 Then
            Do Until PaymentPeriods = 0
            Loop
        End If

        dgvLoanDetails.Rows.Add()
        dgvLoanDetails.Item("Month", i - 1).Value = dgvLoanDetails.Item("Payment", i - 1).Value = FormatCurrency(Payment)
        dgvLoanDetails.Item("Interest", i - 1).Value = FormatCurrency(IntRate)
        dgvLoanDetails.Item("Principal", i - 1).Value = FormatCurrency(Principal)
        dgvLoanDetails.Item("Loan Amount", i - 1).Value = FormatCurrency(LoanAmt)

        
    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        'Click to close.

        Me.Close()
    End Sub

    Private Sub MortgageCaluclator17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        'Clears text boxes

        LoanAmountText.Clear()
        YearsText.Clear()
        InterestRateText.Clear()
        PaymentLabel.Text = ""

    End Sub
End Class





If I remove the datagridview code the 1st part of the program runs fine and works well, it is when I add in the datagridview that the error occers.

Thanks
FinFin

Sorry for the double post. Yes this is my homework but I am not asking for anyone to do it, just to try and help fix what I can not. Also, to dump it down so I can understand what I did wrong.

Thanks
FinFin

Is This A Good Question/Topic? 0
  • +

Replies To: Need DataGridView help.

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5674
  • View blog
  • Posts: 12,186
  • Joined: 02-June 10

Re: Need DataGridView help.

Posted 02 January 2012 - 01:38 PM

Start with this tutorial:
What does this error mean?
It will help you at least determine the line causing the problem.

Second, don't panic over the message but instead calmly read it.

Error 1 Value of type 'Double' cannot be converted to System.Windows.Forms.DataGridViewTextBoxColumn'.

You are trying to take a DataGridViewTextBoxColumn and set it equal to a double value.
Note that you are trying to take the Column... not a value of a cell in the column... but the entire column object... and make it a double. That doesn't make sense.

it would be like trying to set a Textbox equal to a string... Instead of setting the Text property of a textbox to a string.

Does that explanation of the error make sense?


Then it is telling you WHERE the error is
C:\Users\Finley & Angelique\Desktop\Visual studio 2008\Mortgage Calculator CR 17\Mortgage Calcultor CR17\Form1.vb 89 22

Line 89
Character 22
of the Form1.vb file

That makes it pretty easy to find, right?
Was This Post Helpful? 1
  • +
  • -

#3 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: Need DataGridView help.

Posted 07 January 2012 - 03:17 PM

Public Class MortgageCaluclator17
    'Write the program in VB.Net (not Web based) and have it accept user input of the amount, term and interest rate. 
    'Display the mortgage payment amount. Then, list the loan balance and interest paid for each payment over the term 
    'of the loan. The list will be longer than the screen, so use loops to display a partial list, hesitate, and then 
    'display more of the list. Insert comments to document the program.


    Private Sub LoanAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoanAmountText.TextChanged

    End Sub

    Private Sub Years_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YearsText.TextChanged

    End Sub

    Private Sub InterestRate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InterestRateText.TextChanged

    End Sub

    Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        'Length math and input codes will go in thins section.

        'Setting Objects as doubles

        Dim LoanAmt As Double
        Dim Term As Double
        Dim Payment As Double
        Dim IntRate As Decimal
        Dim Principal As Double
        Dim PaymentPeriods As Double
        Dim Years As Double

        'validate loan amount input

        If IsNumeric(LoanAmountText.Text) = False Then
            MsgBox("Ok stupid, how many loan amounts have a letter in it? Just use numbers! Please enter a valid amount.")
            'Loan amount must be numeric. Please enter a valid loan amount.
            LoanAmountText.Clear()
            YearsText.Clear()
            InterestRateText.Clear()
            LoanAmountText.Focus()
            YearsText.Focus()
            InterestRateText.Focus()

            Exit Sub

        End If

        'Setting where to get amounts
        LoanAmt = LoanAmountText.Text
        Years = YearsText.Text
        IntRate = InterestRateText.Text

        'calulate total payment periods

        'Code for the math

        Term = (12 * Years)
        IntRate = (IntRate / 100 / 12)
        Payment = (LoanAmt * IntRate) / (1 - Math.Pow(1 / (1 + IntRate), Term))

        'Sending total monthly payment to Monthly payment label

        PaymentLabel.Text = "" & FormatCurrency(Payment)

        'Sending loan information to the DataGridView

        'Creating a datagridview

        Dim i As Integer
        For i = 1 To Payment

        Next

        Dim dgvLoanDetails As New DataGridView

        'Set Number of Payments

        'Dim Payments As Double
        'Payments = Term

        PaymentPeriods = Term

        'set interest

        IntRate = (LoanAmt * IntRate)

        'set loan amount

        LoanAmt = (LoanAmt - Payment) + IntRate

        'set principle

        Principal = Payment - IntRate

        'clear datagrid view control (removes all rows)

        dgvLoanDetails.Rows.Clear()

        'calculate monthly payment and return value

        'Payment = (LoanAmt * Math.Pow((IntRate / 12) + 1, (PaymentPeriods)) * IntRate / 12) / (Math.Pow(IntRate / 12 + 1, (PaymentPeriods)) - 1)

        'making loop for the DataGridView

        If PaymentPeriods > 1 Then
            Do Until PaymentPeriods = 0
            Loop
        End If

        dgvLoanDetails.Rows.Add()
        dgvLoanDetails.Item("Month", i - 1).Value = FormatNumber(PaymentPeriods)
        dgvLoanDetails.Item("Payment", i - 1).Value = FormatCurrency(Payment)
        dgvLoanDetails.Item("Interest", i - 1).Value = FormatCurrency(IntRate)
        dgvLoanDetails.Item("Principal", i - 1).Value = FormatCurrency(Principal)
        dgvLoanDetails.Item("Loan Amount", i - 1).Value = FormatCurrency(LoanAmt)

    End Sub



Thanks for the help.

I now have a new issue, fixed many things and a new issue arises.

I was told my loop is never ending???? How do I fix this?

Thanks for all the help!
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Need DataGridView help.

Posted 07 January 2012 - 03:38 PM

        If PaymentPeriods > 1 Then
            Do Until PaymentPeriods = 0
            Loop
        End If


If payment periods is greater than 1 loop until it equals 0. This is an infinite loop because within this loop payment periods is never having anything subtracted. Henceforth the loop will run infinitely.
An example of a loop that would terminate:
        If PaymentPeriods > 1 Then
            Do Until PaymentPeriods = 0
            PaymentPeriods = PaymentPeriods - 1
            Loop
        End If


Because paymentperiods is having te value of 1 subtracted from it each time the loop runs, the loop will exit because eventually, PaymentPeriods will become 0.
The loop you had was one to avoid.
Was This Post Helpful? 2
  • +
  • -

#5 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: Need DataGridView help.

Posted 07 January 2012 - 03:38 PM

Sorry I forgo the ending code tag.
Was This Post Helpful? 0
  • +
  • -

#6 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: Need DataGridView help.

Posted 07 January 2012 - 03:54 PM

View PostjimmyBo, on 07 January 2012 - 03:38 PM, said:

The loop you had was one to avoid.


That seems to be the same thing I am feeling about my abilities right now. Thank you for the help.

Now I need to get the rest of it to work. But I will save that for another day.

tlhIn`toq,

Thanks for the information was a big help.

thanks
Fin
Was This Post Helpful? 0
  • +
  • -

#7 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: Need DataGridView help.

Posted 11 January 2012 - 08:55 PM

Everyone,

Thanks for the help!

I am almost done. All I need help with now is sending the results to the datagridview.

Any ideas. If I need to repost my code I can.

Thanks
FinFin
Was This Post Helpful? 0
  • +
  • -

#8 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Need DataGridView help.

Posted 11 January 2012 - 09:03 PM

You'd need to add a new row, then set the values like this:
DataGridView1.Columns(0).SubItems(0).Value = "value"

Try that. I can't because I am at home and on an iPhone - well away from a compiler. Anyways, if that works...
Was This Post Helpful? 1
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Need DataGridView help.

Posted 11 January 2012 - 09:08 PM

@ jimmyBo: datagrid view has no subItem property!!!
You can use
DataGridView1.Item(x, y).Value = "Value"

Where x and y represents row and column index respectively.
Was This Post Helpful? 1
  • +
  • -

#10 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: Need DataGridView help.

Posted 11 January 2012 - 09:12 PM

This is what I have so far. Jimmybo, I think this is what you sugjested but it still is not working.

Thanks for the help.

 

Private Sub LoanAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoanAmountText.TextChanged

    End Sub

    Private Sub Years_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YearsText.TextChanged

    End Sub

    Private Sub InterestRate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InterestRateText.TextChanged

    End Sub

    Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
        'Length math and input codes will go in thins section.

        'Setting Objects as doubles

        Dim LoanAmt As Double
        Dim Term As Double
        Dim Payment As Double
        Dim IntRate As Decimal
        Dim Principal As Double
        Dim PaymentPeriods As Double
        Dim Years As Double

        'validate Loan Amount & Years & Interest Rate input

        If IsNumeric(LoanAmountText.Text) = False Then
            MsgBox("Ok stupid, how many Loan Amounts have a letter in it? Just use numbers! Please enter a valid amount.")
            'Loan amount must be numeric. Please enter a valid loan amount.
            LoanAmountText.Clear()
            YearsText.Clear()
            InterestRateText.Clear()
            LoanAmountText.Focus()
            YearsText.Focus()
            InterestRateText.Focus()

            Exit Sub

        End If

        If IsNumeric(YearsText.Text) = False Then
            MsgBox("Ok stupid, how many Years have a letter in it? Just use numbers! Please enter a valid amount.")
            'Loan amount must be numeric. Please enter a valid loan amount.
            LoanAmountText.Clear()
            YearsText.Clear()
            InterestRateText.Clear()
            LoanAmountText.Focus()
            YearsText.Focus()
            InterestRateText.Focus()

            Exit Sub

        End If

        If IsNumeric(InterestRateText.Text) = False Then
            MsgBox("Ok stupid, how many Interest Rates have a letter in it? Just use numbers! Please enter a valid amount.")
            'Loan amount must be numeric. Please enter a valid loan amount.
            LoanAmountText.Clear()
            YearsText.Clear()
            InterestRateText.Clear()
            LoanAmountText.Focus()
            YearsText.Focus()
            InterestRateText.Focus()

            Exit Sub

        End If

        'Setting where to get amounts
        LoanAmt = LoanAmountText.Text
        Years = YearsText.Text
        IntRate = InterestRateText.Text

        'calulate total payment periods

        'Code for the math

        Term = (12 * Years)
        IntRate = (IntRate / 100 / 12)
        Payment = (LoanAmt * IntRate) / (1 - Math.Pow(1 / (1 + IntRate), Term))

        'Sending total monthly payment to Monthly payment label

        PaymentLabel.Text = "" & FormatCurrency(Payment)

        'Sending loan information to the DataGridView

        'Creating a datagridview

        Dim i As Integer
        For i = 1 To PaymentPeriods

        Next

        Dim dgvLoanDetails As New DataGridView

        'Set Number of Payments

        'Dim Payments As Double
        'Payments = Term

        PaymentPeriods = Term

        'set interest

        IntRate = (LoanAmt * IntRate)

        'set loan amount

        LoanAmt = (LoanAmt - Payment) + IntRate

        'set principle

        Principal = Payment - IntRate

        'clear datagrid view control (removes all rows)

        dgvLoanDetails.Rows.Clear()

        'calculate monthly payment and return value

        'Payment = (LoanAmt * Math.Pow((IntRate / 12) + 1, (PaymentPeriods)) * IntRate / 12) / (Math.Pow(IntRate / 12 + 1, (PaymentPeriods)) - 1)

        'Setting up Columns for DGV
        dgvLoanDetails.ColumnCount = 5
        dgvLoanDetails.ColumnHeadersVisible = True

        ' Set the column header style.
        Dim columnHeaderStyle As New DataGridViewCellStyle()

        columnHeaderStyle.BackColor = Color.Beige
        columnHeaderStyle.Font = New Font("Arial", 12, FontStyle.Bold)
        dgvLoanDetails.ColumnHeadersDefaultCellStyle = columnHeaderStyle

        'Set the column header names.

        dgvLoanDetails.Columns(0).Name = "Month"
        dgvLoanDetails.Columns(1).Name = "Payment"
        dgvLoanDetails.Columns(2).Name = "Interest"
        dgvLoanDetails.Columns(3).Name = "Principal"
        dgvLoanDetails.Columns(4).Name = "Loan Amount"

        'making loop for the DataGridView

        While PaymentPeriods > 1
            Do Until PaymentPeriods = 0
                PaymentPeriods = PaymentPeriods - 1
            Loop
        End While

        'Adding rows to columns

        dgvLoanDetails.Rows.Add()
        dgvLoanDetails.Item("Month", i - 1).Value = FormatNumber(PaymentPeriods)
        dgvLoanDetails.Item("Payment", i - 1).Value = FormatCurrency(Payment)
        dgvLoanDetails.Item("Interest", i - 1).Value = FormatCurrency(IntRate)
        dgvLoanDetails.Item("Principal", i - 1).Value = FormatCurrency(Principal)
        dgvLoanDetails.Item("Loan Amount", i - 1).Value = FormatCurrency(LoanAmt)

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        'Click to close.

        Me.Close()
    End Sub

    Private Sub MortgageCaluclator17_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        'Clears text boxes

        LoanAmountText.Clear()
        YearsText.Clear()
        InterestRateText.Clear()
        PaymentLabel.Text = ""

    End Sub
End Class




Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Need DataGridView help.

Posted 11 January 2012 - 09:23 PM

What didnt work? Explain to us what is your problem with it, we cant follow all previous post to know what has been suggested and what you have tried. Tell us if you have error or unexpected output, and what were you expecting.

Always ask question smaty way
Was This Post Helpful? 1
  • +
  • -

#12 FinFin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 02-January 12

Re: Need DataGridView help.

Posted 11 January 2012 - 09:29 PM

Sorry,

With the code above I wanted the results to run inside the datagridview. The top part of the code works fine, I get the results I want.

When the code runs a list of each payment with interest paid, principal paid, remaining loam amount and payment number in the datagridview. It does not. I am not sure why it is not being sent to the datagridview.

Tried something and by accident the code ran without error, but nothing in the datagridview.

I hope this helps. And thanks to everyone for keeping the information understandable to a noob.

Thanks
FinFin
Was This Post Helpful? 0
  • +
  • -

#13 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Need DataGridView help.

Posted 11 January 2012 - 09:36 PM

Wait a minute, wait a minute. Stop the show.
You're declaring a DataGridView, instancing, etc...
But you're doing something wrong.
You are not adding the declared DataGridView to the form. By not doing this, it won't be visible. Why keep declaring an empty DataGridView every time the calcbutton is pressed and calling clear on it as well? You're resetting values and not displaying them.
Was This Post Helpful? 1
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Need DataGridView help.

Posted 11 January 2012 - 09:41 PM

You are creating a new data gridview every time and not adding it to the form!!!!

What you need to do is creating datagridview once and just adding new rows when needed.

Also what is this loop for?
 For i = 1 To PaymentPeriods
	 
	        Next

It will just loop for nothing and then use i here:
dgvLoanDetails.Item("Month", i - 1).Value = FormatNumber(PaymentPeriods)

Wont it be the same everytime?
Was This Post Helpful? 1
  • +
  • -

#15 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Need DataGridView help.

Posted 11 January 2012 - 10:05 PM

View Postsmohd, on 12 January 2012 - 02:08 PM, said:

@ jimmyBo: datagrid view has no subItem property!!!
You can use
DataGridView1.Item(x, y).Value = "Value"

Where x and y represents row and column index respectively.

I thought I was wrong…
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »