开发者

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="ser开发者_如何转开发ver" AllowPaging="True" PageSize="10" EnableViewState="True"
            AllowSorting="False" DataKeyField="DUEDATE" OnItemDataBound="DG_Grid_ItemDataBound" OnItemCommand="DG_Grid_ItemCommand">

Ideas anyone?


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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜