Solved

Sorting a GridView

Posted on 2009-07-11
6
591 Views
Last Modified: 2012-05-07
Hello,

I am using a GridView with the AllowSorting="True" and the method OnSorting="gv_Sorting" implemented.
Still, When trying to filter the table and keep the sorting:

if (ViewState["SortDirection"] != null)
            {
                if (ViewState["SortDirection"].ToString() == "Ascending")
                {
                    gv.Sort(ViewState["SortExp"].ToString(), SortDirection.Ascending);
                }
                else
                {
                     gv.Sort(ViewState["SortExp"].ToString(), SortDirection.Descending);
                }
            }

When reaching gv.Sort I get the exception: "The GridView 'gv'  fired event Sorting which wasn't handled."

Why is that happening?

Thanks
0
Comment
Question by:Relegence
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:stormist
ID: 24835302
try erasing and re-writing where you invoke the event. Make sure you press tab twice to fill in the event method that it takes you to your handler.
0
 

Author Comment

by:Relegence
ID: 24838021
I am creating the event in design mode.
I have removed it and added it again.
That didn't help.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24840194
How are you filtering the table?  Are you binding to an SqlDataSource, or a DataView?
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:Relegence
ID: 24843371
I am binding to a DataView, nased on a DataTable.
Here is the relevant code:

DataView dvTopFeed = new DataView(dt);
            gv.DataSource = dvTopFeed;
            gv.DataBind();

            if (ViewState["SortDirection"] != null)
            {
                if (ViewState["SortDirection"].ToString() == "Ascending")
                {
                   
                    gv.Sort(ViewState["SortExp"].ToString(), SortDirection.Ascending);
                }
                else
                {
                 
                    gv.Sort(ViewState["SortExp"].ToString(), SortDirection.Descending);
                }
            }
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 24843524
If you are binding to a DataView, then set the DataView.Sort property instead:

    DataView dvTopFeed = new DataView(dt);
    dvTopFeed.Sort = ViewState["SortExp"].ToString();
    gv.DataSource = dvTopFeed;
    gv.DataBind();

0
 

Author Closing Comment

by:Relegence
ID: 31602529
Working great! thanks a lot!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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