System.IndexOutOfRangeException: Cannot find column [duplicate]
I am implementing girdview sorting, which is part of a user control. The code below gives me Indexoutofrange error.
The error message is:
dtView.Sort = strSort;
Errormessage: System.IndexOutOfRangeException: Cannot find column TEXT_COUNTY_ID.
Can anyone point out what I am doing wrong?
Really appreciate your help.
protected void SortGridData_Hkl(Object sender, GridViewSortEventArgs e)
        {
            GridView _dgd_work_onoff = (GridView)Page.FindControl("bodyuc$dgd_work_onoff");
            DataSet dstemp;
            DataView dtView;
            if (ViewState["dsfetchResults"] != null)
            {
                dstemp = (DataSet)ViewState["dsfetchResults"];
                string strSortOrder = ViewState["SortOrder"].ToString();
                if (strSortOrder == "DESC")
                {
                    strSortOrder = "ASC";
                    ViewState["SortOrder"] = strSortOrder;
                }
                else
                {
                    strSortOrder = "DESC";
                    ViewState["SortOrder"] = strSortOrder;
         开发者_如何学编程       }
                string strSort = e.SortExpression.ToString() + " " + strSortOrder;
                ViewState["SortString"] = strSort;
                dtView = dstemp.Tables[0].DefaultView;
                dtView.Sort = strSort;
                if (dtView.Count != 0)
                {
                    if (_dgd_work_onoff != null)
                    {
                        _dgd_work_onoff.DataSource = dtView;
                        _dgd_work_onoff.DataBind();
                    }
                }
            }
dsfetchResults is supposed to contain the data from the database.
I fixed it. the issue was that I used a different column name in the design (.aspx) than the one in the database.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论