1
Answer

gridview sorting in asp.net using c#

chandu gummadi

chandu gummadi

11y
1.8k
1
HI friends iam developing grid view sorting application in asp .net.
iam writting bellow code but its not working properly.

grid view sorting is done for one column and sorting is not done for another column it shows error like  "INVALID COLUMN EMPNAME"

PLEASE SOLVE MY PROBLEM.

public void BindGVPasswordChange()
    {

        try
        {
            DataTable dt = new DataTable();
            Hashtable htnew = new Hashtable();
            htnew.Add("PwdId", 0);
            dt = _sec.getDataTable("Stp_UM_FillGV_Changepasswords", CommandType.StoredProcedure, htnew);
            if (dt.Rows.Count > 0)
            {
                DataView dv = dt.DefaultView;
                if (ViewState["SortDirection"] != null)
                {
                    sortDirection = ViewState["SortDirection"].ToString();
                }
                if (ViewState["SortExpression"] != null)
                {
                    sortExpression = ViewState["SortExpression"].ToString();
                    dv.Sort = string.Concat(sortExpression, " ", sortDirection);
                }
                GVChangePwd.DataSource = dv;
                GVChangePwd.DataBind();
               
            }

            GVChangePwd.Visible = true;
        }
        catch (Exception ex)
        {
        }
       
    }


protected void GVChangePwd_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"] == null || ViewState["SortExpression"].ToString() != e.SortExpression)
        {
            ViewState["SortDirection"] = "ASC";
            GVChangePwd.PageIndex = 0;
        }
        else if (ViewState["SortDirection"].ToString() == "ASC")
        {
            ViewState["SortDirection"] = "DESC";
        }
        else if (ViewState["SortDirection"].ToString() == "DESC")
        {
            ViewState["SortDirection"] = "ASC";
        }
        ViewState["SortExpression"] = e.SortExpression;
        BindGVPasswordChange();
 
    }
Answers (1)