Solved

Gridview sort based on [SortOrder] in the SQL

Posted on 2014-07-17
6
372 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
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!

 
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

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

Suggested Solutions

Title # Comments Views Activity
Using the .NET JSON Serializer to create a single string 6 57
history of pages 7 52
Subtract dates in vb.net 6 33
Help with modifying xml file using VB.NET 5 6
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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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