系统.IndexOutOfRangeException:无法找到列XYZ排序GridView

本文关键字:XYZ 排序 GridView IndexOutOfRangeException 系统 | 更新日期: 2023-09-27 18:05:31

我需要添加排序函数GridView中的列。

我已经在GridView上设置了 allowsort -propertytrue并添加了排序表达式到列。

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

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

下面是我的代码-behind-file对于应该能够排序的列,但是我得到错误:

Exception Details: System。IndexOutOfRangeException:找不到列doName .

代码:

string sortingDirection;
DataSet dsProducts = new DataSet();
DataTable dt1 = new DataTable();
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"] ?? "doName";
    }
    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(dt1);
    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(dt1);
    sortedView.Sort = SortField + " " + sortingDirection;
    gvProducts.DataSource = sortedView;
    gvProducts.PageIndex = e.NewPageIndex;
    gvProducts.DataBind();
}
private void BindData()
{
    gvProducts.DataSource = RetrieveProducts();
    gvProducts.DataBind();
}
private DataSet RetrieveProducts()
{
    sql = "Select * from doTable;";
    dsProducts = new DataSet();
    using (OdbcConnection cn =
      new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
    {
        cn.Open();
        using (OdbcCommand cmd = new OdbcCommand(sql, cn))
        {
            OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
            adapter.Fill(dsProducts);
            dt1 = new DataTable();
            dsProducts.Tables.Add(dt1);
        }
    }
    return dsProducts;
}

系统.IndexOutOfRangeException:无法找到列XYZ排序GridView

发生此异常是因为在gvProducts_PageIndexChanging方法中,dt1的值已重置为新的DataTable(),它没有任何行或列。

你必须重新加载dt1.的值

按如下方式修改代码

OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
adapter.Fill(dsProducts);
if (dsProducts.Tables.Count > 0)
{
    dt1 = dsProducts.Tables[0];
}
dtProducts = dt1;
-------------------------------------------
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";
    }
    BindData();
    DataView sortedView = new DataView(dt1);
    sortedView.Sort = e.SortExpression + " " + sortingDirection;
    SortField = e.SortExpression;
    gvProducts.DataSource = sortedView;
    gvProducts.DataBind();
}