Antonio Mailtraq
Antonio Mailtraq

Reputation: 1407

System.IndexOutOfRangeException: Cannot find column XYZ sorting GridView

I need to add a sorting function to a column in a GridView.

I've set the AllowSorting-property on the GridView to true and added sort expression to the column.

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

Unfortunately, the sorting isn't working in the GridView.

Below is my code-behind-file for the column that should be able to sort, but I get the error:

Exception Details: System.IndexOutOfRangeException: Cannot find column doName.

Code behind:

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;
}

Upvotes: 1

Views: 5537

Answers (1)

Hamamelis
Hamamelis

Reputation: 2115

This exception occured because in gvProducts_PageIndexChanging method, value of dt1 has been reset to new DataTable(), it doesn't have any rows or columns.

So you have to reload value of dt1.

Change you code as follow.

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

Upvotes: 3

Related Questions