Saturday September 04 , 2010

Archive for April, 2009

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