Sorting and Paging not works in GridView

Anonymous
2021-07-06T19:42:47.457+00:00

Hi, I try to use a function to sort and paging, but doesn't works, in in the "design mode" appear the paging, but when I run the program, doesn't appear nothing.
112334-captura.png
and when I run it:
112178-captura1.png

C#:

private string SortDirection  
        {  
            get { return ViewState["SortDirection"] != null ? ViewState["SortDirection"].ToString() : "ASC"; }  
            set { ViewState["SortDirection"] = value; }  
        }  
        private void BindGrid(string sortExpression = null)  
        {  
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
            using (SqlConnection con = new SqlConnection(constr))  
            {  
                using (SqlCommand cmd = new SqlCommand("SELECT distinct No_curso, Room, Idioma, Fecha_inicio, Fecha_final, Training, PMax from [Agregar_sesion]"))  
                {  
                    using (SqlDataAdapter sda = new SqlDataAdapter())  
                    {  
                        cmd.Connection = con;  
                        sda.SelectCommand = cmd;  
                        using (DataTable dt = new DataTable())  
                        {  
                            sda.Fill(dt);  
                            if (sortExpression != null)  
                            {  
                                DataView dv = dt.AsDataView();  
                                this.SortDirection = this.SortDirection == "ASC" ? "DESC" : "ASC";  
  
                                dv.Sort = sortExpression + " " + this.SortDirection;  
                                GridView1.DataSource = dv;  
                            }  
                            else  
                            {  
                                GridView1.DataSource = dt;  
                            }  
                            GridView1.DataBind();  
                        }  
                    }  
                }  
            }  
        }  
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
        {  
            if (e.Row.RowType == DataControlRowType.DataRow)  
            {  
                SqlConnection con = new SqlConnection();  
                con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;  
                con.Open();  
                GridView gv = (GridView)e.Row.FindControl("GridView2") ;  
                string curso = e.Row.Cells[1].Text.ToString();  
                SqlCommand cmd = new SqlCommand("SELECT distinct AgS.No_curso, AgS.Fecha_inicio, M.Num_empleado, M.Nombre,M.Leader From Agregar_sesion Ags, Maestro M WHERE  Ags.No_emp = M.Num_empleado and Ags.No_curso ='" + curso + "'",con);  
                SqlDataAdapter da = new SqlDataAdapter(cmd);  
                DataSet ds = new DataSet();  
                da.Fill(ds);  
                con.Close();  
                gv.DataSource = ds;  
                gv.DataBind();  
            }  
        }  
        protected void OnSorting(object sender, GridViewSortEventArgs e)  
        {  
            this.BindGrid(e.SortExpression);  
  
        }  
  
        protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)  
        {  
            GridView1.PageIndex = e.NewPageIndex;  
            this.BindGrid();  
        }  

HTML:

112270-gv1.png

112246-gv2.png

Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,096 Reputation points
    2021-07-08T03:41:17.81+00:00

    Hi @Anonymous ,
    In your codes,you don't specify what field to sort by. So it will sort by the first field on default. I have changed the sort of No_curso and it works fine with Sorting. You could check your data.
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,096 Reputation points
    2021-07-07T03:28:35.36+00:00

    Hi @Anonymous ,
    How many data in your datebase? I have run your codes and I have 11 records. It works fine.Your pagesize is 10. So,the records need more than 10 to show the paging.
    Or do you have other codes?
    My codes tested is like this:
    112296-capture.png
    112411-capture1.png
    112421-capture2.png
    112394-capture3.png
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.