Sort Substring of GridView in ASP.NET?
I have a column in my GridView which pulls in an individual's name. Unfortunately, the name is contained in one column in the database - both first and last. I'd like to sort on the last name, not the first...is there a way for me to tell the GridView to sort based on a substring? For example, names in this column might be:
- John Doe
- Jane Heck
- Mike Beck I'd like to somehow grab everything after the space in each column and sort on that, ignoring the开发者_JAVA技巧 first name, or even better - using the substring consisting of the first name to do sorts where the last name is identical.
If you can include the LastName into your data source, you could do something like this
<asp:boundfield datafield="FullName"
headertext="Full Name"
sortexpression="LastName"/>
If you are using SQL, then you can include
SUBSTRING(FullName, CHARINDEX(' ', FullName) + 1, LEN(FullName)) AS [LastName]
in you select.
精彩评论