Solved

I need help sorting my GridView in ASP.Net with C# codebehind.

Posted on 2009-05-06
6
835 Views
Last Modified: 2012-05-06
Hi all,
I have an ASP.Net application with C# codebehind.  I am using a gridview in my application which I populate with a string array.  The first column is a the Date column.  I need to sort the grid by Date descending.  How can I do this?

Thanks in advance,
mainrotor
0
Comment
Question by:mainrotor
  • 4
6 Comments
 
LVL 9

Expert Comment

by:tculler
ID: 24320680
I've never done any ASP, but I am aware of a .Sort() method. It takes a sort expression(the column(s) to sort by I believe), followed by an enumeration called System.Web.UI.WebControls.SortDirection. The only two options are, of course, Ascending and Descending.

Hope that helped,
Nate
0
 
LVL 9

Expert Comment

by:tculler
ID: 24320688
Sorry, wasn't specific enough I just realized--the "Sort" method is an instance method that belongs to the GridView object. Call that method on the object you need sorted, and pass in the name of the first column as the expression, followed by SortDirection.Descending.
0
 

Author Comment

by:mainrotor
ID: 24320975
tculler,
This is how I am trying to implement the sort method, but I keep getting an error:

            GridView3.DataSource = t;
            GridView3.DataBind();
            GridView3.Width = 700;
            GridView3.AllowSorting = true;
            GridView3.Sort("Date", SortDirection.Descending);

Here is the error message:

<!-- System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.Web.HttpException: The GridView 'GridView3' fired event Sorting which wasn't handled.

What's happening?  Thanks in advance,
mainrotor
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 9

Expert Comment

by:tculler
ID: 24321195
Is "Date" the name of your field? Usually, that's a restricted field name, I think... I may be wrong.

Also, does the debugger give you a line number that the exception was thrown from?
0
 
LVL 9

Accepted Solution

by:
tculler earned 250 total points
ID: 24321249
Well, I don't have data to test it on, but I did find a way around the error. Just register a method with the "Sorting" event, such as the following:
GridView3.Sorting += delegate(System.Object, System.Web.UI.WebControls.GridViewSortEventArgs gridArgs)
{
     // Honestly, not sure what goes here, heh. However, it does prevent the exception...
};

Open in new window

0
 
LVL 3

Assisted Solution

by:channa_m
channa_m earned 250 total points
ID: 24338577
Include this in the GridView definition int he .aspx

<asp:GridView ID="gv1" ......
            AllowSorting="True" OnSorting="gv1_Sorting">


  protected void gv1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortKey = e.SortExpression;
            // Let's setup the sort expression
            if ( ViewState["orderDirection"].ToString() == "ASC")
            {
                ViewState["orderDirection"].ToString() = "DESC";
            }
            else
            {
                ViewState["orderDirection"].ToString() = "ASC";
            }
            ViewState["sortKey"] = sortKey;
            BindGrid();
        }

private void  BindGrid(){
     DataView dv =  // Get the dataview for the text
                if (ViewState["sortKey"] != null)
                {
                    string sortKey = ViewState["sortKey"].ToString();
                    dv.Sort = sortKey + "  " + orderDirection.Text;

                }
                gv1.DataSource = dv;
gv1.DataBind()

}Include this in the GridView definition int he .aspx

<asp:GridView ID="gv1" ......
            AllowSorting="True" OnSorting="gv1_Sorting">


  protected void gv1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortKey = e.SortExpression;
            // Let's setup the sort expression
            if ( ViewState["orderDirection"].ToString() == "ASC")
            {
                ViewState["orderDirection"].ToString() = "DESC";
            }
            else
            {
                ViewState["orderDirection"].ToString() = "ASC";
            }
            ViewState["sortKey"] = sortKey;
            BindGrid();
        }

private void  BindGrid(){
     DataView dv =  // Get the dataview for the text
                if (ViewState["sortKey"] != null)
                {
                    string sortKey = ViewState["sortKey"].ToString();
                    dv.Sort = sortKey + "  " + orderDirection.Text;

                }
                gv1.DataSource = dv;
gv1.DataBind()

}
0

Featured Post

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

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

840 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