开发者

c# asp.net gridview not sorting

开发者 https://www.devze.com 2023-03-08 10:34 出处:网络
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq;
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)
     开发者_StackOverflow   {
            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?


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?


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


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.


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();
}


just check allowsorting property on aspx page in gridview control

It may be false.

please make it true and then check sorting.

0

精彩评论

暂无评论...
验证码 换一张
取 消