I want to clear the entire contents so that when a different selection is made on one of the checkboxes, only that data is displayed when the calculate button is depressed.

Thanks in advance!!

Here's my code:

Public Class LoanCalc


        Private Sub butCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCalc.Click
            'This procedure executes when the user clicks the Calculate
            'button to produce a loan payment based on the requirements
            'as stated in the service request.

            'Variable Declarations
            Dim douPrincipal As Double = Me.txtPrincipal.Text
            Dim douInterest() As Double = {5.35, 5.5, 5.75}
            Dim decYears() As Decimal = {7, 15, 30}
            Dim douPayment As Double
            Dim douRemainingBalance As Double
            Dim douInterestPaid As Double
            Dim intNumberPayments As Integer
            Dim intPaymentNumber As Integer


            If Me.CheckBox1.Checked Then


                'set the initial balance to the principal amount
                douRemainingBalance = douPrincipal

                'Calculate the payment
                douPayment = ((douPrincipal * (douInterest(0) / 100) / 12) / (1 - Math.Pow(1 + (douInterest(0) / 100) / 12, -(decYears(0) * 12))))
                intNumberPayments = decYears(0) * 12
                'calculate payment, principal, accruedInterest, remainingbalance
                For intPaymentNumber = 1 To intNumberPayments

                    'Calculates the interest paid with each payment and the remaining balance            
                    douInterestPaid = douRemainingBalance * (douInterest(0) / 100 / 12) 'interest paid for current month is the (remaining balance)*(the monthly interest rate... i.e. the interest rate, as a dec., divided by 12)
                    douRemainingBalance = douRemainingBalance - (douPayment - douInterestPaid) ' subtract the princial payment portion of the monthly payment amount from the remaining balance

                    'Append data to the dataGridView 
                    DataGridView1.Rows.Add(intPaymentNumber, FormatCurrency(douPayment, 2), FormatCurrency(douPrincipal, 2), FormatCurrency(douInterestPaid, 2), FormatCurrency(douRemainingBalance, 2))

                Next

            ElseIf Me.CheckBox2.Checked Then

                'set the initial balance to the principal amount
                douRemainingBalance = douPrincipal

                'Calculate the payment
                douPayment = ((douPrincipal * (douInterest(1) / 100) / 12) / (1 - Math.Pow(1 + (douInterest(1) / 100) / 12, -(decYears(1) * 12))))
                intNumberPayments = decYears(1) * 12
                'calculate payment, principal, accruedInterest, remainingbalance
                For intPaymentNumber = 1 To intNumberPayments

                    'Calculates the interest paid with each payment and the remaining balance            
                    douInterestPaid = douRemainingBalance * (douInterest(1) / 100 / 12) 'interest paid for current month is the (remaining balance)*(the monthly interest rate... i.e. the interest rate, as a dec., divided by 12)
                    douRemainingBalance = douRemainingBalance - (douPayment - douInterestPaid) ' subtract the princial payment portion of the monthly payment amount from the remaining balance

                    'Append data to the dataGridView 
                    DataGridView1.Rows.Add(intPaymentNumber, FormatCurrency(douPayment, 2), FormatCurrency(douPrincipal, 2), FormatCurrency(douInterestPaid, 2), FormatCurrency(douRemainingBalance, 2))

                Next
            ElseIf Me.CheckBox3.Checked Then
                'set the initial balance to the principal amount
                douRemainingBalance = douPrincipal

                'Calculate the payment
                douPayment = ((douPrincipal * (douInterest(2) / 100) / 12) / (1 - Math.Pow(1 + (douInterest(2) / 100) / 12, -(decYears(2) * 12))))
                intNumberPayments = decYears(2) * 12
                'calculate payment, principal, accruedInterest, remainingbalance
                For intPaymentNumber = 1 To intNumberPayments

                    'Calculates the interest paid with each payment and the remaining balance            
                    douInterestPaid = douRemainingBalance * (douInterest(2) / 100 / 12) 'interest paid for current month is the (remaining balance)*(the monthly interest rate... i.e. the interest rate, as a dec., divided by 12)
                    douRemainingBalance = douRemainingBalance - (douPayment - douInterestPaid) ' subtract the princial payment portion of the monthly payment amount from the remaining balance

                    'Append data to the dataGridView 
                    DataGridView1.Rows.Add(intPaymentNumber, FormatCurrency(douPayment, 2), FormatCurrency(douPrincipal, 2), FormatCurrency(douInterestPaid, 2), FormatCurrency(douRemainingBalance, 2))

                Next
            End If
        End Sub

        Private Sub butEXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butEXIT.Click
            'This procedure exits the program when the user clicks the EXIT button
            Me.Close()
        End Sub
End Class

Dani AI

Generated

Useful follow-up notes and a few patterns to avoid gotchas when you clear/repopulate a DataGridView (thanks to for the update and for the moderation reminder).

If the grid is data‑bound (BindingSource, DataTable, List(Of T) etc.), clear the underlying source rather than trying to manipulate the grid's Rows collection. Clearing the DataTable or the bound list and then resetting bindings updates the grid cleanly:

' Clear a bound grid backed by a BindingSource/DataTable
Dim bs = TryCast(DataGridView1.DataSource, BindingSource)
If bs IsNot Nothing Then
    Dim dt = TryCast(bs.DataSource, DataTable)
    If dt IsNot Nothing Then
        dt.Clear()
        bs.ResetBindings(False)
    Else
        bs.Clear()   ' for list-based sources
    End If
Else
    Dim dtDirect = TryCast(DataGridView1.DataSource, DataTable)
    If dtDirect IsNot Nothing Then dtDirect.Clear()
    DataGridView1.DataSource = New DataTable() ' fallback reset
End If

For grids you populate entirely in code (unbound), the cleanest pattern is to use a single backing collection (DataTable, BindingList(Of T)) and bind it; then clear that collection on each refresh. If you prefer to rebuild the UI, remove columns first, reset the DataSource, and clear selection to avoid leftover "new row" UI:

Sub ClearGrid(grid As DataGridView)
    grid.SuspendLayout()
    grid.Columns.Clear()
    grid.DataSource = New DataTable()
    grid.AllowUserToAddRows = False
    grid.ClearSelection()
    grid.ResumeLayout()
End Sub

Performance and UX tips: SuspendLayout/ResumeLayout while repopulating, temporarily disable autosizing, call ResetBindings(False) or Refresh() after major changes, and use AllowUserToAddRows = False to hide the placeholder new row. If you see an exception about modifying rows while data‑bound, that signals you must clear the data source instead of the grid itself.

Recommended Answers

All 2 Replies

ISSUE RESOLVED, PLEASE DISREGARD...

This is what I used.

'clear the grid so the information is not appended to the end of each selection
DataGridView1.Rows.Clear()

Source program must be surrounded with BB code tags: See # icon at toolbar and also read How to use bb code tags?.

Mark this thread as "Solved" if you get solution.

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.