RB.
RB.

Reputation: 37192

How do I sort an ASP.NET DataGrid by the length of a field?

I have a DataGrid where each column has a SortExpression. I would like the sort expression to be the equivalent of "ORDER BY LEN(myField)".

I have tried

SortExpression="LEN(myField)" 

but this throws an exception as it is not valid syntax. Any ideas?

Upvotes: 0

Views: 736

Answers (5)

Skizz
Skizz

Reputation: 71070

The SortExpression parameter specifies the name of the column to sort, followed by "ASC" or "DESC" to control the order.

You could change the DataType property of the column to specifiy a user defined type whose comparer function compares string lengths. It won't be a trivial task.

Upvotes: 0

Jason Kostempski
Jason Kostempski

Reputation:

Hmmm. Had some time to test. I was able to get SortExpression="Description.Length" to work. Is this 1.1, 2.0 or 3.5?

Upvotes: 0

Adam Vigh
Adam Vigh

Reputation: 1250

Using Linq, you could write your query like:

query.OrderBy(column => column.MyField.Length);

Upvotes: 0

Ilya Kochetov
Ilya Kochetov

Reputation: 18443

Depending on your SQL flavor the following could work:

SELECT
 ColumnA as FieldA
 , ColumnB as FieldB
 , LEN(ColumnA) as FieldL
FROM TableName
ORDER BY L

And then do

SortExpression="FieldL"

Upvotes: 3

Biri
Biri

Reputation: 7181

What about returning the len by the query already, but don't show that column, only use it as your original column's sortexpression?

I don't think that your idea is supported by default.

Upvotes: 3

Related Questions