Include Datagridview or Datatable as part of Class object?

Hobbyist_programmer 621 Reputation points

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.

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

    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  
        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))  
        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  
            Return Me._prop.Name  
          End Get  
        End Property  
        Public ReadOnly Property Question As String  
            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)  
            Return String.Empty  
          End Get  
        End Property  
        Public Property Answer As Object  
            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  



    0 comments No comments

  2. Hobbyist_programmer 621 Reputation points

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