2

网格视图显示正常,并且按任一列排序都很好。但是,当您单击链接按钮时,返回的 CommandArgument 不是正确的值。就好像 CommandArgument 绑定到行并且没有被排序。

即排序前

Text   Command
abc    A
aaa    B
aab    C

排序后

Text   Command
aaa    A
aab    B
abc    C

单击 aaa 将返回“A”作为参数,而不是应该返回的 B。

aspx 文件中的 GridView 定义如下:

<asp:GridView ID="GridView1" runat="server"
              AllowSorting="True"
              AutoGenerateColumns="False"
              EmptyDataText="No Results to Display"  
              GridLines="None" 
              onsorting="GridView1_Sorting" >
    <Columns>
        <asp:TemplateField HeaderText="File Name" SortExpression="Path">
            <ItemTemplate>
                <asp:LinkButton ID="linkbutton1" runat="server" Text='<%# Eval("Title") %>' OnCommand="LinkButton_Click" CommandArgument='<%# Bind("Path") %>' ></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Document Type" DataField="DocumentCategory (Text)" SortExpression="DocumentCategory (Text)" />
    </Columns>
</asp:GridView>

它绑定到由 sql 查询填充的数据视图并存储在视图状态中

ViewState["GridView1_DataSource"] = ds.Tables[0];
DataView dv = new DataView(ds.Tables[0]);
GridView1.DataSource = dv;
GridView1.DataBind();

我的排序方法是

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    string sortDir = "ASC";
    if (ViewState["SortDirection"] == null)
        ViewState["SortDirection"] = "ASC";

    if (ViewState["SortDirection"].ToString() == "ASC")
    {
        sortDir = "DESC";
        ViewState["SortDirection"] = "DESC";
    }
    else
    {
        ViewState["SortDirection"] = "ASC";
    }

    DataTable dt = (DataTable)ViewState["GridView1_DataSource"];
    DataView dv = new DataView(dt);
    dv.Sort = e.SortExpression + " " + sortDir;
    GridView1.DataSource = dv;
    GridView1.DataBind();
}
4

2 回答 2

1

当您通过 GridView1_Sorting 进行排序时,Page_Load 将被处理,然后是 GridView1_Sorting。所以数据显示正确。单击 LinkBut​​ton 时,Page_Load 在 GridView1__RowCommand 之前处理。通常,Gridview 的 DataBind 位于 Page_Load 中,因此数据现在处于原始顺序。要更正排序,还要在 Page_Load DataBind 中进行排序。

如果(dt.Rows.Count > 0)

{

数据视图 dv = 新数据视图(dt);

dv.Sort = ViewState["SortExpression"].ToString()  + " " + ViewState["SortDirection"].ToString();
GridView1.DataSource = dv;

GridView1.DataBind();

}

我认为您不能对空数据表进行排序。

您可以通过 Debugging 轻松验证这一点,并在 Sorting 和 Page_Load 中添加断点。

于 2012-11-06T00:56:38.103 回答
0

I found the solution. This worked for me. Try this.

 protected void LinkButton_Click(object sender, EventArgs e)
 {
    sortExpr = "Path";
    GridView1_Sorting(null, null);
 } 

and changing this:

string sortExpr;
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
      String sortExpression = null;
        if (sortExpr == null)
        {
            sortExpression = e.SortExpression;

        }
        else
        {
            sortExpression = sortExpr;
        }

    Session["SortExpression"] = sortExpression;

        if (Session["SortDirection"] != null && Session["SortDirection"].ToString() ==    SortDirection.Descending.ToString())
        {
            Session["SortDirection"] = SortDirection.Ascending;
            SortGridview(sortExpression, "ASC");
        }
        else
        {
            Session["SortDirection"] = SortDirection.Descending;
            SortGridview(sortExpression, "DESC");
        }
 }

 private void SortGridview(string sortExpression, string Direction)
    {
        DataView dv = null;
        DataTable dt = GridView1.DataSource as DataTable;
        dv = new DataView(dt);
        dv.Sort = sortExpression + " " + Direction;
        GridView1.DataSource = dv;
        GridView1.DataBind();

    }
于 2014-05-05T16:13:50.813 回答