Solved

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

Posted on 2009-05-06
6
834 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C#, asp.net MVC, sql server, LINQ 3 32
Simple Injector with Web Service 4 42
MediaHelp 4 23
C# bracket error 3 32
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
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.

803 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