0
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

using BiscomFax;


namespace FaxServer
{
    public partial class _Default : System.Web.UI.Page
    {
        public const string vsColumn = "Column";
        public const string vsSortDirection = "SortDirection";

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                ViewState[vsColumn] = "";
            List_Click(ActivityButton, e);
        }

        protected void List_Click(object sender, EventArgs e)
        {
            //using (new Impersonator("administrator", "mlabs.com", "100%secure*"))
            //{
                try
                {
                    Fax fax = new Fax();
                    ConnObj cnObj = GetConfiguration();
                    Button btn = (Button)sender;
                    string sort = "";

                    DataTable dt = new DataTable();

                    switch (btn.CommandName)
                    {
                        case "Activity":
                            sort = "DateTime";
                            dt = fax.GetActivityLog(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
                            break;
                        case "Message":
                            sort = "DateTime";
                            dt = fax.GetMessageStatus(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
                            break;
                        case "Pending":
                            sort = "DeliveryTime";
                            dt = fax.GetPendingList(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
                            break;
                        default:
                            sort = "DateTime";
                            dt = fax.GetActivityLog(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
                            break;

                    }

                    GridView1.DataSource = dt;
                    GridView1.Sort(sort, SortDirection.Descending);
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            //}
        }



        protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {

            DataTable dt = GridView1.DataSource as DataTable;

            if (dt != null)
            {
                DataView dv = new DataView(dt);
                string oldSort = ViewState[vsColumn].ToString();

                dv.Sort = e.SortExpression + " " + convertSorDirectionToSql(e.SortDirection);

                if (dv.Sort == oldSort)
                    dv.Sort = e.SortExpression + " " + convertSorDirectionToSql(SortDirection.Descending);

                ViewState[vsColumn] = dv.Sort;

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

        }

i am having a very difficult time sorting the contents of this gridview, i know that i am binding correctly becuase the data is showing but the data does not get sorted at all by DateTime. what am i doing wrongly?

3
  • When I have done sorting like this before I have not been able to utilize the gridview's datasource property across postbacks. I have had to get the data from the underlying datasource again. Have you tried assigning 'dt' to a fresh copy of the data set? Commented May 26, 2011 at 18:03
  • The only way we could tell for sure what the problem is, you would need to post the aspx content for the grid view -- esp. the template for the date column. Commented May 26, 2011 at 19:42
  • what are the columns in your database? is sorting happening for "DeliveryTime"? Commented May 28, 2011 at 6:16

5 Answers 5

2

Lets look at this line

 dv.Sort = e.SortExpression + " " + convertSorDirectionToSql(e.SortDirection);

While testing, when you put a breakpoint on this line what value did e.SortExpression have?

Sign up to request clarification or add additional context in comments.

3 Comments

Interesting, is DateTime a column in your result set?
Well that is probably your problem, it is a reserved word
@hogan it is not sorting it though
1

Your databind is correct. here is a nice article for sorting in gridview: http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1418

[update]

Your code has several inconsistency. You probably need to implement it again.

  1. allowsorting must be true
  2. each grid column must declare the sortexpression
  3. you can't read the data back from the gridview's datasource
  4. you need to read the data from your database on each postback, i.e. sorting

here is a good example in C#: http://programming.top54u.com/post/ASP-Net-2-0-Gridview-Sorting-Using-C-sharp.aspx

1 Comment

I had much trouble with that too, because there is a lot of points to note. But I'm not remembering all. So it could be better to do it like in the example to avoid problems.
0

You could sort the datatable by setting it's property datatable.defaultview.sort. Don't know of the top of my head whether you need to bind the datatable or the datatable.defaultview after that though.

Comments

0

Try This code

protected void grdList1_Sorting(object sender, GridViewSortEventArgs e)
{
        fillgrid();
        string sortstr = e.SortExpression;
        DataView dview = new DataView(dtable);
        if (sortstr == "asc")
            dview.Sort = e.SortExpression + " desc";
        else
            dview.Sort = e.SortExpression + " asc";
        grdList1.DataSource = dview;
        grdList1.DataBind();
}

Comments

-1

just check allowsorting property on aspx page in gridview control

It may be false.

please make it true and then check sorting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.