【问题标题】:How to sort records in grid view on click of header of a column如何在单击列标题时对网格视图中的记录进行排序
【发布时间】:2014-08-10 21:18:06
【问题描述】:

当用户单击列标题时,我想按列对网格视图进行排序。在这里,用户可以单击任何列,并且网格视图根据单击的列进行排序。这是我的代码:

<asp:GridView ID="gvEmployeeStatus" runat="server" AutoGenerateColumns="false" AllowPaging="True" PageSize="10" AllowSorting="true" OnPageIndexChanging="gvEmployeeStatus_PageIndexChanging" OnSorting="gvEmployeeStatus_Sorting" >

 protected void gvEmployeeStatus_Sorting(object sender, GridViewSortEventArgs e)
    {
        loginName = (String)(Session["LoginName"]);
        dsLoginDetail = clsBLogic.TblLogin(loginName);                
        tblEmployeeNo = dsLoginDetail.Tables[0].Rows[0]["EmployeeNo"].ToString();
        BindDataTogvEmployeeStatus(tblEmployeeNo);

        DataTable dataTable =    gvEmployeeStatus.DataSource as DataTable;
        if(dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection);
            gvEmployeeStatus.DataSource = dataView;
            gvEmployeeStatus.DataBind();
        }

    }

    private string ConvertSortDirection(SortDirection sortDirection)
    {
         string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

网格视图中的记录来自数据集。现在,当我运行代码时,什么也没有发生。我在 gvEmployeeStatus_Sorting 事件上设置了断点来检查它何时会被触发。它没有被解雇。我怎样才能对记录进行排序!

【问题讨论】:

    标签: c# asp.net dataset


    【解决方案1】:

    正如你提到的排序没有触发,我猜你在&lt;asp:TemplateField&gt; or &lt;asp:BoundField&gt;中缺少属性SortExpression

    例如,

     <asp:TemplateField HeaderText="Your Header 1" SortExpression="ColumName1">
    

    <asp:BoundField DataField="Your Header 1" HeaderText="Your Header 1" SortExpression="ColumName1" />
    

    【讨论】:

    • 我已经包含了排序表达式。标题文本带有下划线,页面是回发但记录没有排序。
    • @AgustusCodes : 请问,控件进入gvEmployeeStatus_Sorting()函数?
    • 我在这里持怀疑态度..为了测试这个,我在这个函数中放置了断点但是断点没有进入这里??
    【解决方案2】:

    在 aspx 页面上将此属性设置为 gridview。

    onsortcommand="ComponentGridView_Sorting.

    然后把它放在aspx.cs页面的后端。

    protected void Datagrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
    {
        string strSQL;
        DataTable dt;
        strSQL = "(SQL SELECT STATEMENT HERE)";
        dt = strSQL;
            {
                string SortDir = string.Empty;
                if (dir == SortDirection.Ascending)
                {
                    dir = SortDirection.Descending;
                    SortDir = "Desc";
                }
                else
                {
                    dir = SortDirection.Ascending;
                    SortDir = "Asc";
                }
                DataView sortedView = new DataView(dt);
                sortedView.Sort = e.SortExpression + " " + SortDir;
                Datagrid1.DataSource = sortedView;
                Datagrid1.DataBind();
            }
    }
    protected SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }
    

    【讨论】:

      【解决方案3】:
      #region For Grid view Header Sorting..!!
          public SortDirection dir
          {
              get
              {
                  if (ViewState["dirState"] == null)
                  {
                      ViewState["dirState"] = SortDirection.Ascending;
                  }
                  return (SortDirection)ViewState["dirState"];
              }
              set
              {
                  ViewState["dirState"] = value;
              }
          }
      
          protected void grdAdd_Sorting(object sender, GridViewSortEventArgs e)
          {
              string sortingDirection = string.Empty;
              if (dir == SortDirection.Ascending)
              {
                  dir = SortDirection.Descending;
                  sortingDirection = "Desc";
              }
              else
              {
                  dir = SortDirection.Ascending;
                  sortingDirection = "Asc";
              }
              DataTable dtgrd = AdditionBL.BindAdditionMaster();/**Data Table Bind For Short View**/
              DataView sortedView = new DataView(dtgrd);
              sortedView.Sort = e.SortExpression + " " + sortingDirection;
              grdAddition.DataSource = sortedView;
              grdAddition.DataBind();
          }
          #endregion
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-12
        • 2012-08-09
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多