Xceed DataGrid WPF Object Data Binding

Many of the examples that are provided for the Xceed DataGrid involve binding directly to a database table source, so I wanted to give an example of how easy it is to bind to an Object. Xceed DataGrid makes excellent use of the WPF binding and thus only needs it’s ItemsSource property to be set to a data source that implements IEnumerable.

Guess what? By default, all Generic Lists in the .Net framework are compatible data sources. To show how easy it is to bind the Xceed datagrid to a Generic List of Objects, I have created a sample project (which you can download) that does essentially 3 things:

1. Declares a class named: MyRecord with 4 public properties (First, Last, Title and Phone).
2. Defines a function that creates a Generic List of MyRecord objects, sets temp data for each object and returns the new Generic List of MyRecord objects.
3. In the New() method of the Window that contains the DataGrid, we simply set the ItemsSource to the Generic List of MyRecord objects.

Enjoy!

Class Window1 

    Public Sub New()

        InitializeComponent()

        DataGridControl1.ItemsSource = GetMyRecords()

    End Sub

    Private Function GetMyRecords() As List(Of MyRecord)
        Dim MyRecords As New List(Of MyRecord)

        Dim _myRecord As MyRecord

        _myRecord = New MyRecord
        _myRecord.First = "Carlos"
        _myRecord.Last = "Gonzalez"
        _myRecord.Title = "Managing Director"
        _myRecord.Phone = "213-555-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "Vivien"
        _myRecord.Last = "Dracon"
        _myRecord.Title = "Managing Director"
        _myRecord.Phone = "213-777-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "John"
        _myRecord.Last = "Doe"
        _myRecord.Title = "Developer"
        _myRecord.Phone = "213-111-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "Jane"
        _myRecord.Last = "Doe"
        _myRecord.Title = "Sales"
        _myRecord.Phone = "213-999-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "Bill"
        _myRecord.Last = "Gates"
        _myRecord.Title = "Retired"
        _myRecord.Phone = "213-222-1212"
        MyRecords.Add(_myRecord)

        Return MyRecords
    End Function

    Private Class MyRecord
        Private _first As String
        Private _last As String
        Private _title As String
        Private _phone As String

        Public Property First()
            Get
                Return _first
            End Get
            Set(ByVal value)
                _first = value
            End Set
        End Property

        Public Property Last()
            Get
                Return _last
            End Get
            Set(ByVal value)
                _last = value
            End Set
        End Property

        Public Property Title()
            Get
                Return _title
            End Get
            Set(ByVal value)
                _title = value
            End Set
        End Property

        Public Property Phone()
            Get
                Return _phone
            End Get
            Set(ByVal value)
                _phone = value
            End Set
        End Property
    End Class

End Class
TAGS: , ,


2 Comments

  1. What if I don’t know how many properties/columns do I have for my grid, meaning it is constructed dynamically ?

  2. @ghiutzu pop –

    The grid will auto-set columns for each property you have set on the object that is the basis for the generic list that you are binding as the datasource. So if you don’t know what the properties are until you need to set the grid datasource, the grid will auto-set columns with the headers being the property name. If you want to rename the headers, then you can iterate through the columns and set those values programmatically. Let me know if you need any more information.