zoukankan      html  css  js  c++  java
  • GridView自动排序

    GridView的功能很强大,可以实现很多的功能,下面我讲下GridView自动排序的实现方法。

     /// <summary>
        
    /// GridView排序
        
    /// </summary>
        
    /// <param name="sender"></param>
        
    /// <param name="e"></param>

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        
    {


            
    if (GridViewSortExpression.Equals(e.SortExpression))
            
    {
                GridViewSortExpression 
    = e.SortExpression;
                
    if (GridViewSortDirection.Equals("ASC"))
                
    {
                    GridViewSortDirection 
    = "DESC";
                    
    this.GridView1.DataSource = SortDataTable(数据源false);
                }

                
    else if (GridViewSortDirection.Equals("DESC"))
                
    {
                    GridViewSortDirection 
    = "ASC";
                    
    this.GridView1.DataSource = SortDataTable(数据源true);
                }

                
    else
                
    {
                    GridViewSortDirection 
    = "ASC";
                    
    this.GridView1.DataSource = SortDataTable(数据源true);
                }

            }

            
    else
            
    {
                GridViewSortDirection 
    = "ASC";
                GridViewSortExpression 
    = e.SortExpression;
                
    this.GridView1.DataSource = SortDataTable(数据源true);
            }

            
    this.GridView1.DataBind();

        }

        
    /// <summary>
        
    /// 排序,并保存排序结果
        
    /// </summary>
        
    /// <param name="dataTable"></param>
        
    /// <param name="isPageIndexChanging"></param>
        
    /// <returns></returns>

        protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
        
    {

            
    if (dataTable != null)
            
    {

                DataView dataView 
    = new DataView(dataTable);

                
    if (GridViewSortExpression != string.Empty)
                
    {

                    
    if (isPageIndexChanging)
                    
    {

                        dataView.Sort 
    = string.Format("{0} {1}", GridViewSortExpression, "ASC");

                    }


                    
    else
                    
    {

                        dataView.Sort 
    = string.Format("{0} {1}", GridViewSortExpression, "DESC");

                    }

                    
    //if (Session["FileListTable"] != null)  
                    
    //    Session["FileListTable"] = GetDataTable(dataView);        

                }


                
    return dataView;

            }


            
    else
            
    {

                
    return new DataView();

            }


        }


     

  • 相关阅读:
    什么是web框架
    编写CGI程序步骤
    web开发 c/s结构 和 b/s结构
    python自带的web服务器
    自制计算器
    条件判断
    模版继承
    参数传递
    异常处理
    【uoj#244】[UER #7]短路 CDQ分治+斜率优化dp
  • 原文地址:https://www.cnblogs.com/ringwang/p/1054250.html
Copyright © 2011-2022 走看看