Solved

Gridview sort based on [SortOrder] in the SQL

Posted on 2014-07-17
6
371 Views
Last Modified: 2014-07-20
GW1 gets data from a stored procedure with SQL like "Select SortOrder, F1, F2 From Table1 Order By SortOrder;".

Question: How can I force this gridview to display per SortOrder field?

The field [SortOrder] will not displayed on the GW1. There is no need for additional sort after the page is displayed.

Sort order remains frozen.; I am using Visual Studio 2010, c#.

I wonder something like this could be salvaged to do the job:
protected void gvItems_Sorting(object sender, GridViewSortEventArgs e)
{
    GridView gv = (GridView)sender;
    SqlDataSource ds = (SqlDataSource)gv.DataSource;
    ds.SelectCommand = ds.SelectCommand + " order by " 
        + e.SortExpression + " ASC ";
    gvItems.DataSource = ds;
    gvItems.DataBind();
}

Open in new window


' not sure how to tailor this to work with my application. I got this from:  stackoverflow.com/questions/138412/how-to-sort-columns-in-an-asp-net-gridview-if-using-a-custom-datasource

Thank you.
0
Comment
Question by:Mike Eghtebas
  • 3
  • 3
6 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40203940
A GridView should have a property - AllowSorting - if you set it to false (properties, design view) then you should not need to do anything.

See:
http://msdn.microsoft.com/en-us/library/hwf94875%28VS.80%29.aspx
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40203997
re:>  AllowSorting - if you set it to false

I did that but it is not sorting per Order By m.[SortOrder] field included in the stored proc SQL.

BTW, the field [SortOrder] is not displayed in the gridview because there is no need to see it. Does this effect the outcome?

Thanks,

Mike
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40204026
>>SQL like "Select SortOrder, F1, F2 From Table1 Order By SortOrder;".

that query should return 3 fields (SortOrder, F1, F2) and sorted on the first field.  Are you saying that only 2 fields are returned and the records are not sorted ?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40204027
>>BTW, the field [SortOrder] is not displayed in the gridview because there is no need to see it. Does this effect the outcome?

No, sorting the underlying data source does not require the fields the sort uses to be returned.  (Unlike sorting at the grid when they must be returned, else how could the grid access them to sort by?)
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40204040
re> that query should return 3 fields (SortOrder, F1, F2) and sorted on the first field.  Are you saying that only 2 fields are returned and the records are not sorted ?

Yes. I have include [SortOrder] intentionally in the SQL other than in Order By SortOrder, to make sure it would work. Unless we find a way to do via the property sheet, is it possible to put together a simplified version of the code in my original post to do the job?

Thank you,

Mike
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40204045
My bad, I ran a query on the table in SQL Server and found the problem.

Mike
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can Angular be used with classic asp.net? 3 29
C# Service FileSystemwatcher isse 9 46
Selenium and Xpath 4 29
Starting to use Git with Visual  Studio Online 1 19
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question