0

私のグリッドビューには4つの列があります。任意の列をクリックすると、その列に対して 1 回だけ並べ替えが行われます。どこが間違っていますか?

これが私のコードです:

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Bind();
} 

}

public void bind()
{
String connectionString = WebConfigurationManager.ConnectionStrings["ABConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString); 
con.Open();
DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter("select * from Table1",con);
try
{
da.Fill(ds, "Table1");

GridView1.DataSource = ds.Tables["Table1"];
Session["dttable"] = ds.Tables["Table1"];
GridView1.DataBind();
}
catch (Exception ex)
{

ex.ToString();

}


protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{

string sortExpression = e.SortExpression;
string direction = string.Empty;
SortDirection GridViewSortDirection = e.SortDirection;
if (GridViewSortDirection == SortDirection.Ascending)
{

GridViewSortDirection = SortDirection.Descending;
direction = " DESC";

}

else
{
GridViewSortDirection = SortDirection.Ascending;

direction = " ASC";

}
DataTable dt = (DataTable)Session["dttable"];
dt.DefaultView.Sort = sortExpression + direction;
GridView1.DataSource = dt;
GridView1.DataBind();
}
public SortDirection SortDirection
{
get
{
if (ViewState["SortDirection"] == null)
{
ViewState["SortDirection"] = SortDirection.Ascending;
}
return (SortDirection)ViewState["SortDirection"];
}
set
{
ViewState["SortDirection"] = value;
}
}

}
4

1 に答える 1

0

SortDirectionをViewStateに格納するためのプロパティがありますが、ソートコードのどこにもそれを使用しているようには見えません。ここでは、ビューステートアイテムを使用して、SortExpressionとSortDirectionの両方を保持します。

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    if (e.SortExpression.Equals(CurrentSortExpression, StringComparison.OrdinalIgnoreCase))
    {
        // Same sort expression, toggle direction
        // This would probably be better in a private method or extension method
        if (CurrentSortDirection == SortDirection.Ascending)
            CurrentSortDirection  = SortDirection.Descending;
        else
            CurrentSortDirection = SortDirection.Ascending;

    }
    else
    {
        // Different sort expression, default to asc
        CurrentSortExpression = e.SortExpression;
        CurrentSortDirection = SortDirection.Ascending;
    }

    string direction = "ASC";
    if (CurrentSortDirection == SortDirection.Descending)
        direction = " DESC";

    DataTable dt = (DataTable)Session["dttable"];
    dt.DefaultView.Sort = CurrentSortExpression + direction;
    GridView1.DataSource = dt;
    GridView1.DataBind();
}

public SortDirection CurrentSortDirection  // renaming to make it distinguishable
{
    get
    {
        return (SortDirection)(ViewState["SortDirection"] ?? SortDirection.Ascending);
    }
    set
    {
        ViewState["SortDirection"] = value;
    }
}

public string CurrentSortExpression
{
    get
    {
        return ViewState["SortExpression"] ?? "some default expression";
    }
    set
    {
        ViewState["SortExpression"] = value;
    }
}
于 2012-09-27T12:05:05.353 に答える