Solved

WPF DataGrid, sorting does not work

Posted on 2011-03-10
12
1,332 Views
Last Modified: 2012-05-11
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
Comment
Question by:greenwaymgmt
  • 7
  • 5
12 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35110333
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
 

Author Comment

by:greenwaymgmt
ID: 35132213
Okay, I'll start with checking if I have the latest version of WPF...
0
 

Author Comment

by:greenwaymgmt
ID: 35160235
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
 

Author Comment

by:greenwaymgmt
ID: 35160729
Here's basically how I go about things:

public db as DataEntities
datagrid.itemssource = db.table

still, sorting doesn't work. Any ideas?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35161560
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
 

Author Comment

by:greenwaymgmt
ID: 35165372
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 35169554
I am confused, is sorting working or not working?
0
 

Author Comment

by:greenwaymgmt
ID: 35175824
sorting is not working
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35175963
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
 

Author Comment

by:greenwaymgmt
ID: 35183104
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35185671
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
 

Author Comment

by:greenwaymgmt
ID: 35190537
That worked! Thanks a lot for your help!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now