Solved

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

Posted on 2009-05-06
6
828 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
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 teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now