none
ASP.NET Data Presentation Controls FAQ: Sorting dan paging dalam kontrol GridView bila tidak menggunakan kontrol data source RRS feed

  • Diskusi Umum

  •  

    Terkadang Anda ingin mengikat kontrol GridView pada data tanpa menggunakan kontrol data source seperti SqlDataSource atau control ObjectDataSource. Ini berarti bahwa sorting dan paging tidak akan ditangani secara otomatis oleh kontrol data source. Dalam rangka menerapkan sorting dan paging, Anda harus menggunakan event PageIndexChanging dan Sorting pada kontrol GridView, seperti yang ditunjukkan dalam contoh berikut:

     

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 

        GridView1.DataSource = SortDataTable(GetYourDataSource(), true); 

        GridView1.PageIndex = e.NewPageIndex; 

        GridView1.DataBind(); 

        

    private string GridViewSortDirection 

        get { return ViewState["SortDirection"] as string ?? "ASC"; } 

        set { ViewState["SortDirection"] = value; } 

     

    private string GridViewSortExpression 

        get { return ViewState["SortExpression"] as string ?? string.Empty; } 

        set { ViewState["SortExpression"] = value; } 

     

    private string ToggleSortDirection() 

        switch (GridViewSortDirection) 

        { 

            case "ASC"

                GridViewSortDirection = "DESC"

                break

            case "DESC"

                GridViewSortDirection = "ASC"

                break

        } 

        return GridViewSortDirection; 

     

    protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging) 

        if (dataTable != null

        { 

            DataView dataView = new DataView(dataTable); 

            if (GridViewSortExpression != string.Empty) 

            { 

                if (isPageIndexChanging) 

                { 

                    dataView.Sort = string.Format("{0} {1}",  

                                  GridViewSortExpression,GridViewSortDirection); 

                } 

                else 

                { 

                    dataView.Sort = string.Format("{0} {1}"

                                  GridViewSortExpression,ToggleSortDirection()); 

                } 

            } 

        return dataView; 

        } 

        else 

        { 

            return new DataView(); 

        } 

     

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) 

        GridViewSortExpression = e.SortExpression; 

        int pageIndex = GridView1.PageIndex; 

        GridView1.DataSource = SortDataTable(GetYourDataSource(), false); 

        GridView1.DataBind(); 

        GridView1.PageIndex = pageIndex; 

    }

     

    Thread terkait

    Dynamically sort in the GridView control

    How to sort and page in the GridView control

     

     


    Agnes Sannie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Senin, 17 Oktober 2011 09.29
    Moderator