1

データのページングを実行しようとしています。基本的に、私はデータを取得していて、それを複数のページに表示したいと考えています。しかし、それは機能していません。コーディングには Asp.net と C# を使用しています。データベースとしてmysqlを使用しています。

コードは次のとおりです: ASP コード

<asp:DataGrid runat="server" ID="RestData"
     AllowPaging="True" PageSize="2" 
     OnPageIndexChanged="RestData_PageIndexChanged" AllowCustomPaging="True" 
     PagerStyle-Wrap="False">
     <PagerStyle />
</asp:DataGrid>

C# コード:

protected void Page_Load(object sender, EventArgs e)
 {
    BindData();
 }
public void BindData()
    {
     RestData.DataSource = call.GetReader(Convert.ToInt32(AreaData.SelectedValue));  
       //GetReader is function which returns the data reader of mysql 
     RestData.DataBind();
    }
protected void RestData_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
       RestData.CurrentPageIndex = e.NewPageIndex;
        BindData();
   }

出力:2行を表示しています(ページサイズ2を指定したため)。しかし、次のページを見ることができません。クエリは2行以上を返す必要があります(リピーターを使用すると発生しますが、ページングを行うことができません.

解決策を提供してください(このフォーラムの他の解決策では質問を解決できなかったため、新しい解決策を作成しました)

前もって感謝します。

4

6 に答える 6

1

追加してみてください

If(! IsPostBack)
{
   BindData();
}

ここにあるものと同様の例がありますDataGridカスタムページング

Gridview以下の部分は、よりも適していますDataGrid

イベントPageIndexChangingではなく、サブスクライブする必要があります。PageIndexChanged

PageIndexChangingページングされたボタンの1つをクリックしたとき、グリッドがページング操作を処理する前に発生します

PageIndexChangedが操作をポストしている間。

また、バインドデータのページ読み込みをチェックします

于 2012-05-23T12:31:42.987 に答える
1

ページを変更しようとすると、DataGrid は何をすべきかを認識している必要があります。

あなたのグリッドで(例はVBの私のプロジェクトから来ています、申し訳ありません):

 <asp:DataGrid ID="MainGrid"
    OnEditCommand="MainGrid_Edit"
    OnCancelCommand="MainGrid_Cancel"
    OnUpdateCommand="MainGrid_Update"
    runat="server"
    AutoGenerateColumns="False"
    AllowSorting="True"
    CssClass="DistPortalDataGrid"
    AllowPaging="True"
    CellPadding="3"
    PageSize="25" 
    onpageindexchanging="MainGrid_PageIndexChanged"
    ....

最後の行に注目してください...

コード ビューに移動し、次のサブを作成します。

Protected Sub MainGrid_PageIndexChanged(source As Object, e As DataGridPageChangedEventArgs) Handles MainGrid.PageIndexChanged
    MainGrid.CurrentPageIndex = e.NewPageIndex
    BindMainGrid() 'rebinds the grid
End Sub

Rajuu Parmarに示されているよう.PageIndexに、DataGrid のプロパティはありません。正解です。DataGrid の同等のプロパティは.CurrentPageIndex. なぜ Microsoft がそれらを違うものにしたのか、私にはわかりません。あなたが問題を解決している間 (日付から数年前)、あなたまたは他の誰かがこれが役立つことを願っています.

于 2015-06-08T17:40:14.633 に答える
0

次のようにして、アイテムの総数をグリッドに伝える必要もあります。

public void BindData() {
    RestData.VirtualItemCount = CountTotalItemsInDb();
    // ... the rest ....
}
于 2012-05-23T12:32:43.683 に答える
-1

これを試して。

protected void G_BgtPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //Next page in the GridView
    G_BgtPersonnel.PageIndex = e.NewPageIndex;
    BindgrdPersonnal();
}
于 2012-05-23T12:31:21.530 に答える
-1

Thanks for your help. I got the solution. Just posting the solution if some one need in future. Finally I got the solution by GridView. It does do paging.

ASP Code

<asp:GridView ID="RestGridData" runat="server"
     AllowPaging="True"  AutoGenerateColumns="False"
     PageSize="2" onpageindexchanging="RestGridData_PageIndexChanging">
  </asp:GridView> 

CS Code

protected void Page_Load(object sender, EventArgs e)
    {
       GridBindData(); // on page load 
    }
public void GridBindData()
    {
        MySqlConnection Conn; // I am using MySql 
        myConn = new MySqlConnection("server=localhost;user=root;database=DBName;");
        conn.Open();
        MySqlCommand cmd = new MySqlCommand("Select Name, address, mobileNo, emailID from  user", conn);
         MySqlDataReader reader = cmd.ExecuteReader();
        // As DataReader can move only in forward it is not useful to use it GridView.
       // So convert it to DataTable(or?). It can move in both direction

        DataTable dTable = new DataTable();
        dTable.Load(reader); // ASP function to convert reader to DataTable

        RestGridData.DataSource = dTable; 
        RestGridData.DataBind();

    }
protected void RestGridData_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        RestGridData.PageIndex = e.NewPageIndex;
        GridBindData();
    }
于 2012-05-24T06:06:50.123 に答える