1

このグリッドビューの問題を何日も解決しようとしています。

ヘッダーをクリックしてソートできる、異なる情報を持つ 2 つの異なるグリッドビューを取得しました。情報を整理するためのページング機能もあります。それに加えて、グリッドビューにあるすべての情報を検索できる検索ボタンもあります。ただし、page_load グリッドビューの場合のみ、ページ/並べ替え機能は機能しますが、検索されたグリッドビューはページ/並べ替えができませんでした。

1 つのデフォルト グリッドビューに対してページ/並べ替え機能を実行する方法を紹介します。

まず、このようなデータセットの下にこれらのグリッドビューをバインドし、ページの読み込み時に情報を表示する必要があります

Session["gridview"] = DataBindByDataSet();
GVPolice.DataSource = Session["gridview"];
GVPolice.DataBind();

using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                connAdd.Open();

                var sql = "select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'";
                using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
                {
                    DataSet dsSel = new DataSet();
                    cmdAdd.Fill(dsSel);
                    GVPolice.DataSource = dsSel;
                    GVPolice.DataBind();
                }

次に、データテーブルと呼ばれる別のメソッドの下にグリッドビューをバインドします

private DataTable DataBindByDataSet()
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                //conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
                conn.Open();

                DataSet ds = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter("select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'", conn);
                da.Fill(ds);

                conn.Close();

                 return ds.Tables[0];               

   }

以下は基本的に、ページの読み込み時に表示されるデータをソートする方法です

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = Session["gridview"] as DataTable;

        if (dt != null)
        {
            //Sort the data.
            dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
            // dt.DefaultView.Sort = e.SortExpression.ToString();
            this.GVPolice.DataSource = Session["gridview"];
            GVPolice.DataBind();
        }
    }

    private string GetSortDirection(string column)
    {
        // By default, set the sort direction to ascending.
        string sortDirection = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }

これがページングに関する賢明な方法です

    protected void GVPolice_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GVPolice.PageIndex = e.NewPageIndex;
        GVPolice.DataBind();
    }

これが私が情報を検索した方法です

protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        //conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
        conn.Open();

        DataSet ds = new DataSet();

        SqlDataAdapter da = new SqlDataAdapter("select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available' and " + ddlCategory.SelectedItem.Text + " like '%" + txtData.Text + "%'", conn);
        da.Fill(ds);

        GVPolice.DataSource = ds.Copy();
        GVPolice.DataBind();

        conn.Close();
    }

検索済みボタンからわかるように、グリッドビュー全体を再バインドします。これは、並べ替え/ページコードを再度呼び出す必要があると思いますが、残念ながらそうではありません。実際、ソート/ページングしようとすると、表示された page_load 情報が表示されます。検索試行が行われたときに、検索されたグリッドビューだけのページ/並べ替えを有効にする方法について誰かが教えてくれれば幸いです。

よろしく。

4

1 に答える 1

2

次のように、if ブロック内にページ ロード コードを配置したことを確認します。

if (!IsPostBack)
{
    Session["gridview"] = DataBindByDataSet();
    GVPolice.DataSource = Session["gridview"];
    GVPolice.DataBind();

    using (var connAdd = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    {
        connAdd.Open();

        var sql = "select policeid as [Police ID], fullname as [Full Name], contact as [Contact], email as [Email], nric as [NRIC],  address as [Address], handle as [HandleCase], postedto as [Posted To] from PoliceAccount where status='available'";
        using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
        {
            DataSet dsSel = new DataSet();
            cmdAdd.Fill(dsSel);
            GVPolice.DataSource = dsSel;
            GVPolice.DataBind();
        }
        .....
        .....
    }
}

を使用してイベント内のデータを並べ替えてbtnSearch_Clickいるため、フィルター処理されたデータを次のようにセッションに保存します。Session["gridview"]GridView1_Sorting

protected void btnSearch_Click(object sender, EventArgs e)
{
    .......
    .......

    GVPolice.DataSource = ds.Copy();
    GVPolice.DataBind();

    Session["gridview"] = ds.Tables[0];

    conn.Close()
}

これで、フィルタリングされたデータを並べ替えることができます。

フィルタリングされたデータに対してページングが適切に機能するようにするには、次のようにGVPolice_PageIndexChangingイベントを変更する必要があります。

protected void GVPolice_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GVPolice.PageIndex = e.NewPageIndex;
    GVPolice.DataSource = Session["gridview"];
    GVPolice.DataBind();
}
于 2013-10-30T09:29:42.173 に答える