Here is what I ended up doing
.aspx
<asp:GridView ID="gridAnnoncer" runat="server" AutoGenerateColumns="false" DataSourceID="dsAnnonceSearch" DataKeyNames="Id" AllowPaging="true" AllowSorting="true" PageSize="1">
<Columns>
..
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="dsAnnonceSearch" runat="server" AutoPage="false" OnSelecting="AnnonceSearchOnSelecting">
</asp:LinqDataSource>
.aspx.cs
protected void AnnonceSearchOnSelecting(object sender, LinqDataSourceSelectEventArgs e)
{
using (TheContext context = new TheContext())
{
int? totalRows;
string orderby = e.Arguments.SortExpression.ToLower().Replace(" desc", "").Replace(" asc", "").Trim();
string sortDirection = e.Arguments.SortExpression.ToLower().Contains("desc") ? "desc" : "asc";
e.Result = context.AnnonceSearch("test", orderby, sortDirection, e.Arguments.StartRowIndex, e.Arguments.MaximumRows, out totalRows);
e.Arguments.TotalRowCount = (int) totalRows;
}
}
Stored procedure
ALTER PROCEDURE [dbo].[AnnonceSearch]
@keywords nvarchar(4000),
@orderby varchar(100),
@orderDirection varchar(100),
@startRowIndex int,
@maximumRows int,
@totalRows int output
AS
BEGIN
SET NOCOUNT ON;
if @keywords is null or @keywords = '' set @keywords = '""'
if @startRowIndex < 0 RAISERROR('startRowIndex parameter is invalid', 0, 1)
if @maximumRows < 1 RAISERROR('getRows parameter is invalid', 0, 1)
select TOP (@maximumRows) Id, Productname, description, Zipcode from
(select row_number() over (order by
case when lower(@orderDirection) = 'desc' then
case lower(@orderby)
when 'description' then Annoncer.description
when 'Productname' then Annoncer.Productname
end
end desc,
case when lower(@orderDirection) = 'asc' then
case lower(@orderby)
when 'description' then Annoncer.description
when 'Productname' then Annoncer.Productname
end
end
) as RowNumber,
Annoncer.Id, Annoncer.Productname, Annoncer.description from Annoncer
where @keywords = '""' or (contains(Annoncer.Productname, @keywords) or
contains(Annoncer.description, @keywords))) searchResult
where RowNumber > @startRowIndex
SELECT @totalRows = COUNT(*) FROM Annoncer
END