question

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 asked ·

Execute or Initilize a Class??

Hallo

I have a Bindinglist of Objects bound to a datagridview. I populate this list with a Sub. Problem is these list of objects are not initialized unless i click or activate the datagridview. Is there a way to initilize these objects so they do their work without activating them first?.

Thanks

dotnet-visual-basic
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, impossible to say without seeing your code. You could look at one of my code sample, front-end forms project, backend class project. Note the BindingList in this case is custom but a regular BindingList will produce the same results. Note the code sample was written for another forum question yet should assist possibly with your problem if time is taken to study the code flow.

78469-f1.png


0 Votes 0 ·
f1.png (14.8 KiB)

Hi @Hobbyistprogrammer-7674 ,
Do you consider initilizing objects in ‘Form.Load’ event?

I have a Bindinglist of Objects bound to a datagridview. I populate this list with a Sub.

Could you provide some related code here? It will help us analyze your problem and make a test.
We are waiting for your update.

0 Votes 0 ·

I have attached my sample code below. thanks

0 Votes 0 ·
Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 answered ·

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?


78742-sample.gif




 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

sample.gif (425.3 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered ·

Hi @Hobbyistprogrammer-7674 ,
You can consider using the following remedies:
When you click cell in DataGridView1, TabControl will switch to the TabPage where DataGridView2 is located, and then return to the original TabPage.

     Private pageName As String
     Private pageIsChanged = False
     Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    
         If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = "CTotal" Then
    
             If Not TabControl1.SelectedTab.Name.Equals("TabPage2") Then
                 pageName = TabControl1.SelectedTab.Name
                 TabControl1.SelectedTab = TabControl1.TabPages("TabPage2")
                 pageIsChanged = True
             End If
         End If
     End Sub
     Private Sub DataGridView1_CellMouseLeave(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellMouseLeave
         If pageIsChanged Then
             TabControl1.SelectedTab = TabControl1.TabPages(pageName)
             pageIsChanged = False
         End If
     End Sub

Result of my test :
79465-gif.gif
Hope it could be helpful.

Best Regards,
Xingyu Zhao


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



gif.gif (157.7 KiB)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have the same datasource / bindingsource showing different contents in 3 different datagridviews (hiding few rows in each datagridview) . in this case i can not use this solution. any other way? thanks.

0 Votes 0 ·