使DataGridViewColumn可排序

本文关键字:排序 DataGridViewColumn | 更新日期: 2023-09-27 18:05:12

我在asp.net 4中使用C-sharp。

我需要为GridView中的列添加一个排序函数。我已经将GridView上的AllowSorting -属性设置为true,并在列中添加了排序表达式。

不幸的是,排序在GridView中不起作用。

下面是应该能够排序的列的后台代码,但是我得到了错误

CS1502:最佳重载方法匹配'System.Data.DataView.DataView(System.Data.DataTable)'有一些无效参数

:

DataView sortedView = new DataView(BindData());

代码:

string sortingDirection;    
public SortDirection dir
{
    get
    {
        if (ViewState["dirState"] == null)
        {
            ViewState["dirState"] = SortDirection.Ascending;
        }
        return (SortDirection)ViewState["dirState"];
    }
    set
    {
        ViewState["dirState"] = value;
    }
}
public string SortField
{
    get
    {
        return (string)ViewState["SortField"] ?? "Name";
    }
    set
    {
        ViewState["SortField"] = value;
    }
}
protected void gvProducts_Sorting(object sender, GridViewSortEventArgs e)
{
    sortingDirection = string.Empty;
    if (dir == SortDirection.Ascending)
    {
        dir = SortDirection.Descending;
        sortingDirection = "Desc";
    }
    else
    {
        dir = SortDirection.Ascending;
        sortingDirection = "Asc";
    }
    DataView sortedView = new DataView(RetrieveProducts());
    sortedView.Sort = e.SortExpression + " " + sortingDirection;
    SortField = e.SortExpression;
    gvProducts.DataSource = sortedView;
    gvProducts.DataBind();
}
protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    if (dir == SortDirection.Ascending)
    {
        sortingDirection = "Asc";
    }
    else
    {
        sortingDirection = "Desc";
    }
    DataView sortedView = new DataView(RetrieveProducts());
    sortedView.Sort = SortField + " " + sortingDirection;
    gvProducts.DataSource = sortedView;
    gvProducts.PageIndex = e.NewPageIndex;
    gvProducts.DataBind();
}
private void BindData()
{
    gvProducts.DataSource = RetrieveProducts();
    gvProducts.DataBind();
}
private DataSet RetrieveProducts()
{
    DataSet dsProducts = new DataSet();
    string sql = " ... ";
    using (OdbcConnection cn =
      new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
    {
        cn.Open();
        using (OdbcCommand cmd = new OdbcCommand(sql, cn))
        {
              ........
        }
    }
    return dsProducts;
}

编辑# 1

DataView sortedView = new DataView(dsProducts.Tables[0]);

Edit # 2

我在aspx页面添加了:

<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

但是如果点击列名我有这个新的错误:

System.IndexOutOfRangeException: Cannot find table 0.

:

Line 100:        DataView sortedView = new DataView(dsProducts.Tables[0]);

使DataGridViewColumn可排序

DataView类只有三个构造函数,其中一个是默认构造函数DataView(),第二个是DataTable作为参数DataView(DataTable),另一个是四个参数DataView(DataTable, String, String, DataViewRowState)。

DataView构造函数需要这些类型中的任何一种参数,但是您的代码有其他类型的参数。这就是错误。

你的BindData方法应该返回一个数据表对象,

//This function should return a Datatable
    private void BindData()
    {
        gvProducts.DataSource = RetrieveProducts();
        gvProducts.DataBind();
    }

,你可以在这里传递给你的DataView。

DataView sortedView = new DataView(BindData());

第二次编辑,

System.IndexOutOfRangeException: Cannot find table 0.

:

Line 100:        DataView sortedView = new DataView(dsProducts.Tables[0]);

我猜数据集是空的,错误清楚地表明数据集中没有任何表在位置0。检查你的数据集是否有表。可能是您的sql请求没有得到任何表来填充数据集。否则,您可能已经创建了数据集的新实例。