Solved

Gridview sort based on [SortOrder] in the SQL

Posted on 2014-07-17
6
374 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
Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

 
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

718 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