在网格视图中进行分页和排序

时间:2011-05-10 14:06:59

标签: asp.net sorting gridview pagination

我有一个代码在网格视图中执行分页和排序。

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

帮帮我朋友。

1 个答案:

答案 0 :(得分:0)

这种方法:

public string sort_grid()

不带参数,但你试图用e.SortDirection调用它:

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

您必须将sort_grid()的签名更改为

sort_grid(SortDirection sortDirection)

在解决方法sort_grid之前,这也将解决您尝试使用sortDirection变量的第二个问题。