Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1430
  • Last Modified:

WPF DataGrid, sorting does not work

Hi all,

I have the following data grid which I populate based on the data in tblData.

<DataGrid Name="gridTasks" ItemsSource="{Binding tblTasks}" CanUserSortColumns="True"/>

When I click the column headers, the grid doesn't sort. I originally had a grid with manually defined columns etc. and figured that's where the problem might be, but even after scaling back to this very basic form, I still can't sort.

I'm using ADO.NET as my data source. Data displays properly but sorting doesn't work. Really appreciate your help!

Regards,
Greenway
0
greenwaymgmt
Asked:
greenwaymgmt
  • 7
  • 5
1 Solution
 
Bob LearnedCommented:
It is pretty easy to sort, with the property setting, so it might be a problem with:

1) The version of WPF that you are using

2) The DataGrid binding source
0
 
greenwaymgmtAuthor Commented:
Okay, I'll start with checking if I have the latest version of WPF...
0
 
greenwaymgmtAuthor Commented:
Updated to the newest version of Visual Studio, still not working. How can I go about checking what the problem is with the binding source, please?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
greenwaymgmtAuthor Commented:
Here's basically how I go about things:

public db as DataEntities
datagrid.itemssource = db.table

still, sorting doesn't work. Any ideas?
0
 
Bob LearnedCommented:
I don't see how this is working, since you are binding to "tblTasks", but I don't see any reference to that.  That looks like you are working with Entity Framework generated classes, but that is just a guess.
0
 
greenwaymgmtAuthor Commented:
Hey, sorry yes I am working with Entity Framework generated classes. The datacontext of my entire page is one entry in a master table that has several tasks attached to it in tblTasks. In the first post to this question I explained my original problem, that binding through XAML did not work with sorting. I then investigated further and realized I couldn't even bind the Entity Framework generated classes directly and have sorting work. db.table = db.tblTasks in the above comment.
0
 
Bob LearnedCommented:
I am confused, is sorting working or not working?
0
 
greenwaymgmtAuthor Commented:
sorting is not working
0
 
Bob LearnedCommented:
Can you create a BindingList(Of T) from the entity set?

Example:

Dim employeeList As New BindingList(Of Employee)(Me.DataContext.Employees.ToList)

Open in new window

0
 
greenwaymgmtAuthor Commented:
I tried that, but then I can't make simple queries such as:

employeeList.Where(Function(f) f.Status= "Active")

In addition, instead of accurately displaying the data. The grids now appear all black, with no visible data.
0
 
Bob LearnedCommented:
I finally got around to trying some different experiments, and I believe that I have what you need:

1) Return the ObjectSet(Of Entity), and the sort doesn't work

2) Return a List(Of Entity), and the sort appears to work.

Example:

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data.Objects

	Friend Class DataLayer

		Private _entities As AdventureWorksEntities

		Public Sub New()
			_entities = New AdventureWorksEntities()
		End Sub

		Public ReadOnly Property Products() As List(Of Product)
			Get
				Return _entities.Products.ToList()
			End Get
		End Property

	End Class

Open in new window

0
 
greenwaymgmtAuthor Commented:
That worked! Thanks a lot for your help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now