Vincent Shikomba - 4 months ago 28x

Vb.net Question

Good day,

I've got a class where I calculate mortgage payments for every single month. In this class, I have a data structure where I store the properties of each mortgage payment, which are calculated in a loop. I then have SortedList where I store every mortgage payment. In the class, I have got a function that returns this SortedList.

In my user interface I call the function that returns the SortedList and bind it to a DataSource. I set this DataSource to a DataGridView, but when I execute the program, the DataGridView only displays blank rows with no values.

This is my classs (called Amortization)

And after the End Function is my calling of the function

`Public Function GetMonthlyPaymentDetails() As SortedList(Of Integer, MonthlyPeriod)`

Dim counter As Integer = 1

Period.PaymentDate = Today

Period.CumulativeInterest = 0

Period.EndingBalance = Principal

Period.ScheduledPayment = ComputeScheduledPayment()

Do

Period.BeginningBalance = Period.EndingBalance

Period.Interest = GetInterestPerPayment(Period.BeginningBalance)

Period.Principal = GetPrincipalPerPayment(Period.ScheduledPayment, Period.Interest)

Period.EndingBalance = GetEndingBalance(Period.BeginningBalance, Period.Principal)

Period.CumulativeInterest = AccumilateInterest(Period.CumulativeInterest, Period.Interest)

'ArrayPeriod(counter) = Period

ArrayPeriod.Add(counter, Period)

counter += 1

'Loop Until Period.EndingBalance = 0

Loop Until counter = 240

Return ArrayPeriod

End Function

How function is called

`Dim ABindingSource As New BindingSource`

a.GetMonthlyPaymentDetails()

ABindingSource.DataSource = a.ArrayPeriod

AmortizationGrid.DataSource = ABindingSource

Answer

A `SortedList`

is not a suitable data source for a `DataGridView`

. Each item in the list is a `KeyValuePair`

, so the grid can only show `String`

representations of the `Key`

and the `Value`

. If you expect to see the properties of the `MonthlyPeriod`

objects then you must bind a list of `MonthlyPeriod`

objects. You could get that from the `Values`

property of the `SortedList`

.

Source (Stackoverflow)

Comments