Wpf-Datagrid and ObservableCollection-AutoGenerateColumns?


I have just worked out a solution to bind a property(DP) to Datagrid. Its okay and everything is smooth and soft. But when I run the application, The DataGrid Displays only blank rows!(Say there are 10 records, I see 10 Blank rows without columns!). The property is an ObservableCollection(of Customer).
This is my Xaml:

DataGrid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="DGCustomers" AutoGenerateColumns="True"  ItemsSource="{Binding CustomersOC}"/>


And I request you to take a glance at my codeBehind:

 Public Class CustomersEditor 
 Dim CustomersCollection As New ObservableCollection(Of Customer)
      Public Class Customer
        Public CustomerID As String
        Public CustomerName As String
        Public ContactPerson As String
        Public Address As String
        Public Area As String
        Public City As String
        Public Pincode As String
        Public ContactNumber As String
        Public OnePercentDiscount As Boolean
        Public Sub New(ByVal Cid As String, ByVal Cname As String, ByVal Cperson As String, ByVal Addr As String, ByVal Area1 As String, ByVal Cty As String, ByVal PCode As String, ByVal CNo As String, ByVal OPD As Boolean)
            CustomerID = Cid
            CustomerName = Cname
            ContactPerson = Cperson
            Address = Addr
            Area = Area1
            City = Cty
            Pincode = PCode
            ContactNumber = CNo
            OnePercentDiscount = OPD

        End Sub

    End Class
 Public Shared CustomersColl As DependencyProperty = DependencyProperty.Register("CustomersOC", GetType(ObservableCollection(Of Customer)), GetType(CustomersEditor))
    Public Property CustomersOC As ObservableCollection(Of Customer)
        Get
            Return GetValue(CustomersColl)

        End Get
        Set(ByVal value As ObservableCollection(Of Customer))
            SetValue(CustomersColl, value)

        End Set
    End Property
      Public Sub New()
        InitializeComponent()
      'I have added records to CustomersCollection with Access Database Reader.such as CustomersCollection.Add(new Customer(....))
       SetValue(CustomersColl, CustomersCollection)
       DGCustomers.DataContext = Me


    End Sub
End Class 

Is there any solution such that I can view the DataGrid with Nine Columns and with appropriate data?[AutoGenerateColumns Failed! But my code for fetching data from database is refined,perfect and working.]
for C# users I have got converter.telerik.com

c#wpfdatagrid

Answers

answered 3 years ago Mike Eason #1

Your class should contain Properties, not Variables. The DataGrid will only look for public properties instead of public variables.

See here, and here

comments powered by Disqus