簡體   English   中英

網格視圖中的分頁和排序

[英]paging and sorting in grid view

我有一個代碼在網格視圖中執行分頁和排序。

(bing_grid是用戶定義的函數)

public void bind_grid()
    {
        con = new SqlConnection();
        con.ConnectionString = "Data Source=STIRAPC105;InitialCatalog=anitha;Integrated Security=True";
        con.Open();

        SqlDataAdapter sqa = new SqlDataAdapter("select * from employees", con);
        DataSet ds = new DataSet();
        sqa.Fill(ds);
        DataTable mytab = ds.Tables[0];

        GridView1.DataSource = mytab;
        GridView1.DataBind();
        //con.Close();

    }

分頁代碼

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;

        bind_grid();

    }

排序代碼

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

        DataTable dt = GridView1.DataSource as DataTable;
        if (dt != null)
        {
            DataView dataview = new DataView(dt);
            dataview.Sort = e.SortExpression + " " + sort_grid(e.SortDirection);
            GridView1.DataSource = dataview;
            GridView1.DataBind();
        }
    }

用戶定義的代碼進行排序

public string sort_grid()
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

分頁有效,錯誤為:

1. "no overload for method 'sort_grid' takes 1 argument" (dataview.Sort = e.SortExpression + " " + sort_grid(e.SortDirection);)

2.The name 'sortDirection does not exist in the current context. (switch (sortDirection))

幫幫我啦。

此方法:

public string sort_grid()

不帶任何參數,但是您嘗試使用e.SortDirection調用它:

dataview.Sort = e.SortExpression + " " + sort_grid(e.SortDirection);

您必須將sort_grid()的簽名更改為

sort_grid(SortDirection sortDirection)

這還將解決您的第二個問題,您正在嘗試使用sortDirection變量,然后在方法sort_grid中將其聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM