简体   繁体   中英

Paging Next in DataGrid in Asp.net C# Not Working

I am trying to do paging of data. Basically I am taking a data and want to show it on multi pages. But it is not working. I am using Asp.net and C# for coding. I am using mysql as database.

Code is as follows : ASP code

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

C# code:

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();
   }

Output : It is displaying two rows(as i have given pagesize 2). But I am not able to see next page. The query should returns more than 2 rows(it does happen when i use repeater but i am not able to do paging in it.

Please provide some solution ( I was not able to solve my question with any other solution in this forum so i have created new one)

THANKS in ADVANCE.

Try adding

If(! IsPostBack)
{
   BindData();
}

There is an example similar to what you have over here DataGrid custom paging

The below part is more suited for Gridview rather than for DataGrid

You should be subscribing to PageIndexChanging rather than PageIndexChanged event.

PageIndexChanging happens when you click one of the paged buttons and before the grid handles the paging operation

while PageIndexChanged is post the operation.

Also i would put in a check in the page load for the Bind data

The DataGrid must know what to do when you try to change pages.

In your grid (example comes from my project which is in VB, sorry):

 <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"
    ....

Notice the last line...

Now go to the code view and create the following sub:

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

As indicated to Rajuu Parmar , there is no .PageIndex property for the DataGrid. This is correct. The equivalent property for DataGrid is the .CurrentPageIndex . Why Microsoft made them different, I have no idea. While you solved the problem (years ago from the date), I'm hoping that you or someone else finds this useful.

You also need to tell the grid how many items in total there are, by doing something like:

public void BindData() {
    RestData.VirtualItemCount = CountTotalItemsInDb();
    // ... the rest ....
}

The reason why paging on your datagrid not working was because you had created 'protected' method and it wasn't able to access it. If the code is change to below, it would have worked. Hope it helps others in need.

public void RestData_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
       RestData.CurrentPageIndex = e.NewPageIndex;
        BindData();
   }

try this.

protected void G_BgtPersonnel_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    //Next page in the GridView
    G_BgtPersonnel.PageIndex = e.NewPageIndex;
    BindgrdPersonnal();
}

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM