Problem when multi-column sorting
Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=656
Printed Date: 11-Jun-2026 at 9:20am
Topic: Problem when multi-column sorting
Posted By: krugerm
Subject: Problem when multi-column sorting
Date Posted: 14-Jan-2008 at 10:43pm
Howdy - any advice and / or suggestions are gratefully welcomed! :)
I'm trying to do some multi-column sorting but am being greeted with an ApplicationException: "Object reference no set to an instance of an object."
Scenario: Retrieve a list of Debtors and sort by their NAME and ACCNO, and assign to a BindingSource.
Step One is easy - retrieve & sort:
EntityList<Debtor> mDebtors = mPersMgr.GetEntities<Debtor>(); mDebtors.ApplySort(new DebtorNameAccnoComparer(), true);
These two lines work - mDebtors successfully contains a sorted list of debtors.
Step Two - assign to a BS raises an ApplicationException when I try to assign like so:
this.mDebtorsBS.DataSource = mDebtors;
Object reference not set to an instance of an object. at System.Windows.Forms.BindingSource.BuiltSortString(ListSortDescriptionCollection sortsColln) at System.Windows.Forms.BindingSource.get_InnerListSort() at System.Windows.Forms.BindingSource.set_InnerListSort(String value) at System.Windows.Forms.BindingSource.SetList(IList list, Boolean mdc, Boolean applySortAndFilter) at System.Windows.Forms.BindingSource.ResetList() at System.Windows.Forms.BindingSource.set_DataSource(Object value) at ...
Any suggestions? It works fine if I use a passthru query like so:
PassthruRdbQuery aQuery = new PassthruRdbQuery( typeof(Debtor), "SELECT * FROM DR_ACCS WHERE ISACTIVE='Y' AND NAME<>'' ORDER BY NAME, ACCNO" ); mDebtors = mPersMgr.GetEntities<Debtor>(aQuery); this.mDebtorsBS.DataSource = mDebtors;
...but then I can't dynamically re-sort the list when its contents change. :|
My DebtorNameAccnoComparer looks like this:
public class DebtorNameAccnoComparer : IComparer<Debtor> { public int Compare(Debtor x, Debtor y) { if ((x == null || x.IsNullEntity) && (y == null || y.IsNullEntity)) return 0; if (y == null || y.IsNullEntity) return -1; if (x == null || x.IsNullEntity) return 1;
if (x.Accno == y.Accno) return 0;
int i = string.Compare(x.Name, y.Name, true, System.Globalization.CultureInfo.CurrentCulture); if (i == 0) // name is the same, so additionally use accno to sort return y.Accno > x.Accno ? 1 : -1; else return i; } }
|
Replies:
Posted By: pkarsh
Date Posted: 17-Jan-2008 at 2:38pm
I tried to reproduce this (sort of) and was unable to do so.
I worked against the Employee table in the IdeaBladeTutorial database and I sort against TitleOfCourtesy and LastName. The idea is that there will be more than one person with the same TitleOfCourtesy but, in this case, no two Employees with the same LastName.
The code I use to populate the BindingSource looks like this:
private void Form1_Load(object sender, EventArgs e)
{
mPm = PersistenceManager.DefaultManager;
employees = mPm.GetEntities<Employee>();
employees.ApplySort(new EmployeeTitleNameComparer(), true);
employeeBindingSource.DataSource = employees;
}
My IComparer implementation looks like this:
class EmployeeTitleNameComparer: IComparer<Employee>
{
public int Compare(Employee pEmployee1, Employee pEmployee2)
{
if (pEmployee1 == null || pEmployee1.IsNullEntity && pEmployee2 == null || pEmployee2.IsNullEntity)
{
return 0;
}
if (pEmployee2 == null || pEmployee2.IsNullEntity)
{
return -1;
}
if (pEmployee1 == null || pEmployee1.IsNullEntity)
{
return 1;
}
if (pEmployee1.LastName == pEmployee2.LastName)
{
return 0;
}
int i = (String.Compare(pEmployee1.TitleOfCourtesy, pEmployee2.TitleOfCourtesy, true, System.Globalization.CultureInfo.CurrentCulture));
if (i == 0)
{
return (String.Compare(pEmployee1.LastName, pEmployee2.LastName, true, System.Globalization.CultureInfo.CurrentCulture));
}
else
{
return i;
}
}
}
I don't get the exception and the sort works as I expect.
I suspect that the problem is actually somewhere other than in the sort. Lacking any better ideas, I would suggest that, as a start, you comment out the line where you apply the sort and see if you still get the exception.
|
Posted By: krugerm
Date Posted: 17-Jan-2008 at 4:32pm
Hrm - thanks for trying. :)
To answer your question: If I comment out the sorting line it all works fine - no exception is raised.
The really annoying thing is that I can see the sort works when I step through it - if I peek at employees immediately after the ApplySort() it shows that they've all been sorted correctly! It's only when I try to set it as the DataSource that the exception is raised.
Any further suggestions?
mike
|
Posted By: pkarsh
Date Posted: 17-Jan-2008 at 5:22pm
|
How complicated is the application in which this problem is occurring? Our guru suggests that you try it with the simplest possible application if you have not done so. In my case I have a single entity class with only two properties that are bound using the DataGridViewBindingManager. Also, my comparer class is part of the UI startup project and I don't implement any IComparer in the entity class itself.
|
Posted By: DeLight
Date Posted: 18-Jan-2008 at 1:03am
Try looking via Reflector on some hidden logic in BindingSource class...
As for me, BindingSource class on my machine even don't have System.Windows.Forms.BindingSource.BuiltSortString(ListSortDescriptionCollection sortsColln) method (FW 2.0).
-------------
|
|