Datagridview Not Showing derived class properties?

Hobbyist_programmer 621 Reputation points
2024-03-14T18:46:16.91+00:00

Hallo,

I have a following code and i am trying to get all the properties from the derived class to showup on the datagridview through bindingsource. Any idea why it is not showing up? thanks

Imports System.ComponentModel

Public Class Form1
    Public BS As New BindingSource
    Public equipments As New BindingList(Of Equipment)
    
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        equipments.Add(New Motor With {.Name = "M1", .HorsePower = 10})
        equipments.Add(New Motor With {.Name = "M2", .HorsePower = 20})
        equipments.Add(New Motor With {.Name = "M3", .HorsePower = 30})
        equipments.Add(New Engine With {.Name = "E1", .FuelType = "Diesel"})
        equipments.Add(New Engine With {.Name = "E2", .FuelType = "Petrol"})
        BS.DataSource = equipments
        DataGridView1.DataSource = BS
    End Sub

End Class

Public Class Equipment
    Public Property Name As String
    Public Property Group As String
End Class

' Implement on Motor class
Public Class Motor
    Inherits Equipment
    Implements IEquipment

    Public Property HorsePower As Integer

    Public Function GetSpecifications() As Dictionary(Of String, Object) Implements IEquipment.GetSpecifications
        Dim specs As New Dictionary(Of String, Object) From {
                                                            {"Horse Power", HorsePower}
                                                                                     }
        Return specs
    End Function
End Class

Public Class Engine
    Inherits Equipment
    Implements IEquipment

    Public Property FuelType As String
    Public Function GetSpecifications() As Dictionary(Of String, Object) Implements IEquipment.GetSpecifications
        Dim specs As New Dictionary(Of String, Object) From {
                                                            {"Fuel Type", FuelType}
                                                                                     }
        Return specs
    End Function

End Class

Interface IEquipment
    Function GetSpecifications() As Dictionary(Of String, Object)
End Interface

Also i am trying to dynamically databind derived class properties to textboxes on button click.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        ' Get selected equipment
        'Dim selectedEquipment = CType(selectedRow.DataBoundItem, Equipment)

        Dim selectedEquipment = CType(BS.Current, Equipment)

        ' Get its type  
        Dim equipmentType = selectedEquipment.GetType()

        ' Create instance of that type
        Dim equipment As IEquipment = CType(Activator.CreateInstance(equipmentType), Equipment)

        ' Copy values from selected to new instance
        Dim specs = equipment.GetSpecifications()


        Dim NewX As Integer = 10
        Dim NewY As Integer = 10


        For Each prop In equipmentType.GetProperties()
            prop.SetValue(equipment, prop.GetValue(selectedEquipment))
            Debug.Print(prop.Name & ": " & prop.GetValue(selectedEquipment))

            Dim ValtxtBox As New TextBox
            Dim Speclbl As New Label
            Dim Unitlbl As New Label

            Speclbl.Text = prop.Name
            Speclbl.Location = New Point(NewX, NewY)
            Me.Controls.Add(Speclbl)

            'txtBox.DataBindings.Add("Text", BS, prop.Name)
            ValtxtBox.Location = New Point(NewX + 100, NewY - 3)
            Me.Controls.Add(ValtxtBox)

            NewY += 28

        Next

 End Sub

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,723 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 31,166 Reputation points Microsoft Vendor
    2024-03-18T01:37:17.33+00:00

    Hi @Hobbyist_programmer ,

    You can use reflection to dynamically determine the properties of the derived classes and add them as columns in the DataGridView.

        Private Sub GetProperties()
    
            ' Clear existing columns
    
            DataGridView1.Columns.Clear()
    
            ' Keep track of added columns to avoid duplicates
    
            Dim addedColumns As New HashSet(Of String)
    
            ' Add columns for properties of the base class
    
            For Each prop As PropertyInfo In GetType(Equipment).GetProperties()
    
                If Not addedColumns.Contains(prop.Name) Then
    
                    DataGridView1.Columns.Add(prop.Name, prop.Name)
    
                    DataGridView1.Columns(prop.Name).DataPropertyName = prop.Name
    
                    addedColumns.Add(prop.Name)
    
                End If
    
            Next
    
            ' Iterate through the items in the list to determine additional columns based on the derived class type
    
            For Each item As Equipment In equipments
    
                Dim itemType = item.GetType()
    
                Dim properties = itemType.GetProperties()
    
                For Each prop As PropertyInfo In properties
    
                    ' Exclude properties from the base class
    
                    If prop.DeclaringType IsNot GetType(Equipment) Then
    
                        If Not addedColumns.Contains(prop.Name) Then
    
                            DataGridView1.Columns.Add(prop.Name, prop.Name)
    
                            DataGridView1.Columns(prop.Name).DataPropertyName = prop.Name
    
                            addedColumns.Add(prop.Name)
    
                        End If
    
                        ' Populate cell with corresponding data
    
                        Dim rowIndex As Integer = equipments.IndexOf(item)
    
                        Dim columnIndex As Integer = DataGridView1.Columns(prop.Name).Index
    
                        Dim value As Object = prop.GetValue(item)
    
                        DataGridView1.Rows(rowIndex).Cells(columnIndex).Value = value
    
                    End If
    
                Next
    
            Next
    
        End Sub
    

    Best Regards.

    Jiachen Li


    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.