?
Solved

Gridview sort based on [SortOrder] in the SQL

Posted on 2014-07-17
6
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 44

Accepted Solution

by:
AndyAinscow earned 2000 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

Technology Partners: 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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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