6

私はasp:GridViewコントロールを持っています、それは私がAllowSorting="True"プロパティを設定しました:

<asp:GridView ID="gridUsers" runat="server" PageSize="100" ShowHeaderWhenEmpty="True"
   Width="100%" AllowSorting="True" onrowcreated="gridUsers_RowCreated" 
   onsorting="gridUsers_Sorting">
</asp:GridView>

設計時には、グリッドは並べ替え可能に見えます。

ここに画像の説明を入力してください

ただし、実行時には、真ん中の列のみが並べ替え可能です。

ここに画像の説明を入力してください

asp:GridViewASP.NETで並べ替え可能にするにはどうすればよいですか?


asp:GridViewwithにAllowSorting Sortingイベントハンドラーが存在する必要があります。

protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e)
{
   //asp:GridView will throw an exception if a Sorting event handler isn't present
}

更新:説明列の何が特別なのかを理解しました。これは、データベースからそのまま表示名が正しい唯一の列です。残りの列は、表示名を表示できるように修正する必要があります

protected void gridUsers_RowCreated(object sender, GridViewRowEventArgs e)
{
   e.Row.Cells[0].Visible = false; //UserGUID
   e.Row.Cells[1].Text = "User name";
   e.Row.Cells[2].Text = "Full name";
   //3=Description
   e.Row.Cells[4].Text = "E-mail";
   e.Row.Cells[5].Text = "Active";
       e.Row.Cells[5].Visible = false;
   e.Row.Cells[6].Text = "Account type";
 }

今、私はトリッキーな部分を理解する必要があります。列を並べ替え可能にします。

4

2 に答える 2

1
protected void gridUsers_Sorting(object sender, GridViewSortEventArgs e)
{
    DataTable dt = Session["SortedTable"] as DataTable;

    if (dt != null)
    {

      //Sort the data.
      dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
      gridUsers.DataSource = Session["SortTable"];
      gridUsers.DataBind();
    }
}

このmsdn の例から適応:

于 2012-06-11T21:19:32.537 に答える