views:

223

answers:

2

I have a GridView that is using a LinqDataSource which is tied to a table in my database. This table of mine has a int foreign key. In my presentation layer, using TemplateField in Gridview, i hide the foreign key value and make another call to the database to show the name of it's associated name so it's more readable for the user.

However, when i click on the header of the foreign key to sort its column, it sorts by the id value, rather than the string value of it's associated name. How can i make this Gridview sort by it's name and not by it's foreign key value?

A: 

Set the SortExpression of the key column to the name of the Name column in the data source.

Matthew Jones
A: 

Just to add to what Matthew Jones said you have to specify how that field can be accessed. e.g.

class Parent { public int Id { get; set; } public string Name { get; set; } }

class Child { public int Id { get; set; } public Parent Mom { get; set; } public Parent Dad { get; set; } }

If you were binding a table of 'Child' and you wanted to sort on the Mom property the SortExpression would be "Mom.Name"

Eric