2

グリッドビューは正常に表示され、いずれかの列による並べ替えはうまく機能します。ただし、リンク ボタンをクリックすると、返される CommandArgument は正しい値ではありません。CommandArgument が行にバインドされていて、ソートされていないかのようです。

つまり、ソート前

Text   Command
abc    A
aaa    B
aab    C

ソート後

Text   Command
aaa    A
aab    B
abc    C

aaa をクリックすると、引数として B ではなく "A" が返されます。

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 に格納されます

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 がクリックされると、GridView1__RowCommand の前に Page_Load が処理されます。通常、Gridview の DataBind は Page_Load にあるため、データは元の順序になります。正しい並べ替えを Page_Load DataBind にも配置します。

if (dt.Rows.Count > 0)

{

DataView dv = 新しい DataView(dt);

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

GridView1.DataBind();

}

空のデータテーブルをソートできるとは思いません。

これは、デバッグによって簡単に確認でき、並べ替えと 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 に答える