Solved

Gridview sort based on [SortOrder] in the SQL

Posted on 2014-07-17
6
365 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 33

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 33

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 33

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now