3

Using ASP.NET Dynamic Data with a LINQ to SQL DataContext from the Northwind Database...

When I add a DisplayColumn Attribute to one of my LINQ to SQL entity classes and reference a property from my custom code in the partial class, I lose the ability to sort by that column in the generated GridViews. I continue to lose the ability to sort even if I reference a non-custom property as the sortColumn.

Why is this happening?

Example Code:

[DisplayColumn("LastNameFirstName", "LastName", false)]
public partial class Employee
{
    public string LastNameFirstName
    {
        get { return LastName + ", " + FirstName; }
    }
}

Aaron

EDIT: The sortColumn specifies the column that will be used to sort this entity when it is used as a foreign key (in a DropDownList), not when it is being sorted in the GridView.

Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
Aaron Hoffman
  • 6,604
  • 8
  • 56
  • 61

4 Answers4

2

This may be by Design...

The "sortColumn" specifies the column that will be used to sort this entity when it is used as a foreign key (in a DropDownList), not when it is being sorted in the GridView.

Aaron Hoffman
  • 6,604
  • 8
  • 56
  • 61
2

That is correct because the property is not in the DB and linq to SQL will try to construct a T-SQL quert to the DB to get your entities. but will fail because these is no column with that name.

Wizzard
  • 924
  • 5
  • 9
  • But the second parameter, "LastName", would be in the DB. And if I specify it there, I think it should be trying to sort by that. Either way it could sort by the custom property after it creates the Domain Objects... – Aaron Hoffman Apr 10 '09 at 17:04
  • 1
    Yes but the filed LastNameFirstName is not in the DB and Linq does not know about what goes on inside LastNameFirstName :) – Wizzard Apr 15 '09 at 12:38
0

Try adding [ScaffoldColumn(true)] - it might trick dynamic data to enable sorting

[DisplayColumn("LastNameFirstName", "LastName", false)]
public partial class Employee
{
    [ScaffoldColumn(true)]
    public string LastNameFirstName
    {
        get { return LastName + ", " + FirstName; }
    }
}
Chicago
  • 1,619
  • 4
  • 19
  • 32
0

You could try overriding the ToString() method that might work but it would only filter on the entity referenced by the FK relationship.

Wizzard
  • 924
  • 5
  • 9