Include Datagridview or Datatable as part of Class object?

Hobbyist_programmer 621 Reputation points
2020-12-06T11:13:50.71+00:00

Hallo, I would like to know is there a way to make datagridview or datatable as part of the object. I have a class and i want to include a datagrid view with some rows and columns so user can type their values and comments and only rows with user content should be stored with object.

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

2 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-12-07T09:41:46.127+00:00

    HI,
    based on your other questions, I have created a demo that shows properties and values of an object in a DataGridView. In addition, the properties have attributes that are displayed as an additional column.

    Imports System.ComponentModel  
    Imports System.Reflection  
      
    Public Class Form1  
      
      Private dgv As New DataGridView With {.Dock = DockStyle.Fill, .AutoGenerateColumns = True}  
      
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        Me.Controls.Add(dgv)  
        '  
        Dim s As New Sample  
        '  
        dgv.DataSource = New PropertyList(s)  
      End Sub  
      
      Private Class PropertyList  
        Inherits List(Of PropertyItem)  
        Public Sub New(obj As Object)  
          For Each prop In obj.GetType().GetProperties(BindingFlags.Public Or BindingFlags.Instance)  
            Me.Add(New PropertyItem(obj, prop))  
          Next  
        End Sub  
      End Class  
      
      Public Class PropertyItem  
        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 Question 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 Answer As Object  
          Get  
            Return Me._prop.GetValue(Me._obj)  
          End Get  
          Set(value As Object)  
            Me._prop.SetValue(Me._obj, value)  
          End Set  
        End Property  
      End Class  
      
      Public Class Sample  
        <Description("Question 1")> Public Property Q1 As String  
        <Description("Question 2")> Public Property Q2 As String  
      End Class  
      
    End Class  
    

    Result:

    45723-x.png

    0 comments No comments

  2. Hobbyist_programmer 621 Reputation points
    2020-12-07T11:36:43.487+00:00

    Hallo Peter thanks for the answer , just wondering how to serialize this to xml.