Solved

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

Posted on 2009-05-06
6
838 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
Independent Software Vendors: 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 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
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.

685 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