Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Binding a WPF DataGrid to an ObservableCollection of arrays or am I lost here?

$
0
0

Hi!

Im trying to make a DataGrid show an observable collection. The problem is that the item class, lets call it Person cant look like in all the examples on the web ie. with a bunch of public propertys of strings that will later make up the data for the cells of each row.

The reason that i cant use the "standard model" is that the number of columns (or Propertys) will vary from time to time.

This is what i call the standard model:

Public Class Person
    ' Methods 
    Public Sub New(ByVal first As String, ByVal last As String)
        Me._firstName = first
        Me._lastName = last
    End Sub


    ' Properties 
    Public Property FirstName() As String
        Get
            Return Me._firstName
        End Get
        Set(ByVal value As String)
            Me._firstName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return Me._lastName
        End Get
        Set(ByVal value As String)
            Me._lastName = value
        End Set
    End Property

    ' Fields 
    Private _firstName As String
    Private _lastName As String
End Class

And i use something like this for the ObservableCollection:

Public Class clsVisibleItems
    Inherits System.Collections.ObjectModel.ObservableCollection(Of DynamicRow)

    ' Methods 
    Public Sub New()
        MyBase.Add(New DynamicRow("Willa", "Cather"))
        MyBase.Add(New DynamicRow("Isak", "Dinesen"))
        MyBase.Add(New DynamicRow("Victor", "Hugo"))
        MyBase.Add(New DynamicRow("Jules", "Verne"))
    End Sub

    Public Sub AddItem(pn As DynamicRow)
        MyBase.Add(pn)

    End Sub
End Class

In some cases the datagrid will be initilalized like so:

TimeStamp | Value 1 | Value 18 | Value 36

Some other time:

TimeStamp | Value 2 | Value 3 | Value 8 | Value 12 | Value 66

Could someone perhaps tell me how to bind a gridrow to an array like so:

dim s(x) as string (where x could vary). Then instead of having FirstName and LastName in the class like in the standard model i would dynamically create an array with all the columns needed for the current setup and use something like:

Public Class DynamicRow
    ' Methods 
    Public Sub New(ByVal timestamp As String, ByVal cells() As String())
        Me._timestamp = timestamp
        Me._cells = cells
    End Sub


    ' Properties 
    Public Property Timestamp() As String
        Get
            Return Me._timestamp 
        End Get
    End Property

    Public Property Cells() As String()
        Get
            Return Me._cells
        End Get
    End Property

    ' Fields 
    Private _timestamp As String
    Private _cells As String()
End Class



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>