ASP.NET GridView排序实现&事件处理

时间:2012-01-03 06:12:50

标签: c# asp.net sorting gridview

有人可以分享如何实际实现gridview排序并在以下情况下处理该事件:

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

我仅从代码隐藏构建我的gridview,因此我无法使用默认方法或解决方案。

谢谢

3 个答案:

答案 0 :(得分:6)

这可能就是你要找的东西:

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标题将变为可点击。确保sort表达式属性是您通过sql查询绑定的字段的名称。

希望这有帮助。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

网格视图在asp.net中排序

第一步 向网页添加网格视图通过添加允许排序 true并通过排序启动事件来编辑源代码

<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 只是列名......