Hi, here is my sample code . also you can see the problem in the attached Gif,
basically i pass my properties to another class and do some calculations there and get the total in "Sample" class. If the DGV is active then it returns the total. How can i fix it?
Imports System.ComponentModel
Imports System.Reflection
Imports System.Linq.Expressions
Public Class Form1
Public SampleList As New Samples
Public WithEvents BS_SampleList As New BindingSource
Public CalList As New BindingList(Of Calculation)
Public BS_SampleCalculation As New BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BS_SampleList.DataSource = SampleList
DataGridView1.DataSource = BS_SampleList
BS_SampleCalculation.DataSource = CalList
DataGridView2.DataSource = BS_SampleCalculation
End Sub
Private Class PropertyList
Inherits List(Of Calculation)
Public Sub New(obj As Object)
For Each prop In obj.GetType().GetProperties(BindingFlags.Public Or BindingFlags.Instance)
Me.Add(New Calculation(obj, prop))
Debug.Print(obj.ToString & prop.Name)
Next
End Sub
End Class
Public Class Calculation
Public Sub New(obj As Object, prop As PropertyInfo)
Me.obj = obj
Me.prop = prop
End Sub
Private obj As Object
Private prop As PropertyInfo
Public ReadOnly Property ID As String
Get
Return Me.prop.Name
End Get
End Property
Public ReadOnly Property Description As String
Get
For Each att As CustomAttributeData In Me.prop.CustomAttributes
Dim args = att.ConstructorArguments
If args.Count > 0 Then Return args(0).ToString.Substring(1, args(0).ToString.Length - 2)
Next
Return String.Empty
End Get
End Property
Public Property Quantity As Object
Get
Return Me.prop.GetValue(Me.obj)
End Get
Set(value As Object)
Me.prop.SetValue(Me.obj, CInt(value))
End Set
End Property
Public ReadOnly Property Cost As Integer?
Get
If ID = "TT" Then
Dim Tot As Integer? = Form1.getTot()
prop.SetValue(Me.obj, Tot)
Return Form1.getTot()
Else
If CInt(Me.prop.GetValue(Me.obj)) > 0 Then
Return CInt(Me.prop.GetValue(Me.obj)) * 100
Else
Return 1000
End If
End If
End Get
End Property
End Class
Public Function getTot() As Integer?
Return Aggregate e In CalList Where e.ID Like "I*" Into Sum(e.Cost)
End Function
Public Class Sample
<Description("Item 1")> <Browsable(False)> Public Property I1 As Integer?
<Description("Item 2")> <Browsable(False)> Public Property I2 As Integer?
<Description("Item 3")> <Browsable(False)> Public Property I3 As Integer?
<Description("Total")> <Browsable(False)> Public Property TT As Integer?
Public Property ID As String
Public Property Name As String
Public ReadOnly Property CTotal As Integer?
Get
Return TT
End Get
End Property
End Class
Public Class Samples
Inherits BindingList(Of Sample)
End Class
Private Sub Btn_AddSample_Click(sender As Object, e As EventArgs) Handles Btn_AddSample.Click
SampleList.Add(New Sample With {.ID = 1, .Name = "Sample1"})
End Sub
Private Sub BS_SampleList_CurrentChanged(sender As Object, e As EventArgs) Handles BS_SampleList.CurrentChanged
Dim CurrentS = CType(BS_SampleList.Current, Sample)
If Not CurrentS Is Nothing Then
CalList.Clear()
Dim selected_properties = CurrentS.GetType().GetProperties().Where(Function(p) p.GetCustomAttributes().Any(Function(a) TypeOf a Is DescriptionAttribute))
For Each prop In selected_properties
CalList.Add(New Calculation(CurrentS, prop))
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
DataGridView2.Refresh()
DataGridView1.Refresh()
End Sub
End Class