【问题标题】:ASP.NET GridView Sorting Implementation & Event HandlingASP.NET GridView 排序实现和事件处理
【发布时间】:2012-01-03 06:12:50
【问题描述】:

如果出现以下情况,有人可以分享如何实际实现 gridview 排序和处理该事件:

  1. 数据是手动绑定的
  2. Gridview 是使用模板字段构建的,该模板字段仅从代码后面(而非标记)抽取出来

我完全从代码隐藏构建我的网格视图,因此我不能使用默认方法或解决方案。

谢谢

【问题讨论】:

标签: c# asp.net sorting gridview


【解决方案1】:

这可能是您正在寻找的:

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    listBindByName(); //this would be your procedure to look for the data you want
    DataSet dsSortTable = GridView1.DataSource as DataSet;
    DataTable dtSortTable = dsSortTable.Tables[0];
    if (dtSortTable != null)
    {
        DataView dvSortedView = new DataView(dtSortTable);
        dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
        ViewState["sortExpression"] = e.SortExpression;
        GridView1.DataSource = dvSortedView;
        GridView1.DataBind();
    }
    UpdatePanel1.Update();
}

private string getSortDirectionString()
{
    if (ViewState["sortDirection"] == null)
    {
        ViewState["sortDirection"] = "ASC";
    }
    else
    {
        if (ViewState["sortDirection"].ToString() == "ASC")
        {
            ViewState["sortDirection"] = "DESC";
            return ViewState["sortDirection"].ToString();
        }
        if (ViewState["sortDirection"].ToString() == "DESC")
        {
            ViewState["sortDirection"] = "ASC";
            return ViewState["sortDirection"].ToString();
        }
    }
    return ViewState["sortDirection"].ToString();
}

这是 TemplateField 的示例:

<asp:TemplateField HeaderText="Description" SortExpression="description">
    <ItemTemplate>
        <asp:Label Visible="true" runat="server" ID="descriptionLabel" Text='<%# bind("description")  %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtEditDescription" Width="100px" runat="server" Text='<%#Bind("description") %>' />
    </EditItemTemplate>
</asp:TemplateField>

通过添加 SortExpression 属性,GridView 标题将变为可点击。确保排序表达式属性是您通过 sql 查询绑定的字段的名称。

希望这会有所帮助。

【讨论】:

【解决方案2】:
/* Best to use the shortened routine below - which can be further shortened */        
private string GetSortDirectionString()
{
    if (ViewState["sortDirection"] == null) ViewState["sortDirection"] = "ASC";

    var currDir = ViewState["sortDirection"].ToString().ToUpper();

    switch (currDir)
    {
        case "ASC": ViewState["sortDirection"] = "DESC"; break;
        case "DESC": ViewState["sortDirection"] = "ASC"; break; 
    }

    return ViewState["sortDirection"].ToString();
}

【讨论】:

    【解决方案3】:

    网格视图在 asp .net 中排序

    第一步 添加网格视图到您的页面编辑源代码,添加 allowsorting true 并通过 onsorting

    启动事件
    <asp:GridView ID="GridView1" AllowSorting="true" OnSorting="GridView1_Sorting"  runat="server">
        </asp:GridView>
    

    第二步

    在页面后面的代码中..我们需要处理这个事件“GridView1_Sorting”和数据表绑定。 在页面加载时,我们将数据表与 gridview 绑定

     dt = Class1.getDataSet().Tables[0]; // here dt is the datatable object declared Globally.
     GridView1.DataSource = dt; 
     GridView1.DataBind();
    

    所以现在如果我们运行我们的代码,网格视图将是可见的,但没有排序。

    第三步

    接下来我们需要处理 Gridview 排序事件。 首先我们需要声明一个静态字符串 SortDirection。

     protected void SetSortDirection(string sortDirection)
            {
                if (sortDirection == "ASC")
                {
                    _sortDirection = "DESC";
                }
                else
                {
                    _sortDirection = "ASC";
                }
            } 
    

    所以sortDirection是一个静态字符串...这个函数我们用来在升序和降序之间切换... 第 4 步

     protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            SetSortDirection(SortDirection);
            if (dt != null)
            {
                //Sort the data.
                dt.DefaultView.Sort = e.SortExpression + " " + _sortDirection;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                SortDireaction = _sortDirection;
            }
        }
    

    所以我们已经完成了排序...... sortExpression 只是列名......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      相关资源
      最近更新 更多