简体   繁体   中英

DataGrid in UserControl

I have a DataGrid in a USerControl. Somehow the paging doesnt work, the paging has the right amount of pages, but clicking the numbers does not work ... it stays on page 1. This is my Grid:

<asp:DataGrid ID="DG_Grid" runat="server" AllowPaging="True" PageSize="10" EnableViewState="True"
            AllowSorting="False" DataKeyField="DUEDATE" OnItemDataBound="DG_Grid_ItemDataBound" OnItemCommand="DG_Grid_ItemCommand">

Ideas anyone?

Use like this:

 private void FillGrid(int aPageNumber)
    {
       //Insert you code here


        DG_Grid.DataSource = _ds.Tables[0].DefaultView;
        DG_Grid.PageIndex = aPageNumber;
        DG_Grid.DataBind();
    }

And after this call this event of grid

 protected void DG_Grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        FillGrid(e.NewPageIndex);
    }

On Load call:

 FillGrid(0);

Have you handled PageIndexChanged event? See here for more details.

Is the Usercontrols' Datagrid binded in the Page or in the Usercontrol itself? I think you bind them in the page and you dou sort them in the page. So you have to raise the PageIndexChanged Event from the UserControl and handle it in the page.

This exmaple is with GridView but for DataGrid its the same.

In the UserControl define an event that you handle in your Page:

    Public Event GridPageChanged(ByVal grid As GridView)

    Private Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
        RaiseEvent GridPageChanged(Me.GridView1)
    End Sub

You can now catch the usercontrol's GridPageChanged in your Page and do the sorting.

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