【问题标题】:paging and sorting in grid view网格视图中的分页和排序
【发布时间】:2011-08-22 12:28:55
【问题描述】:

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

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

帮助我的朋友。

【问题讨论】:

    标签: asp.net sorting gridview pagination


    【解决方案1】:

    这个方法:

    public string sort_grid()
    

    不带参数,但您尝试使用 e.SortDirection 调用它:

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

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

    sort_grid(SortDirection sortDirection)
    

    这也将解决您尝试使用 sortDirection 变量的第二个问题,然后在方法 sort_grid 中声明它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2013-02-17
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      相关资源
      最近更新 更多