Solved

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

Posted on 2009-05-06
6
841 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
[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
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

752 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