zoukankan      html  css  js  c++  java
  • gridview sort


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
               GridView1.DataSource= getDate();
               GridView1.DataBind();
            }
        }

        public DataTable getDate()
        {
            DataTable dt;
             string conString="server=server01;database=test;uid=sa;pwd=sql";
             using (SqlConnection con = new SqlConnection(conString))
             {
                 con.Open();
                 using(SqlCommand cmd = new SqlCommand("select * from tab",con))
                 {
                    using(SqlDataReader reader=cmd.ExecuteReader())
                    {
                        dt = new DataTable();
                        //填充数据
                        dt.Load(reader, LoadOption.OverwriteChanges);
                    }
                 }
             }
             return dt;
            
        }
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            if ((string)this.ViewState["Sort"]!= e.SortExpression)
            {
                this.ViewState["Sort"] = e.SortExpression;
            }
            else
            {
                this.ViewState["Sort"] = e.SortExpression + " " + "DESC";
            }

            DataTable dt = new DataTable();
            dt = getDate();
            dt.DefaultView.Sort = ViewState["Sort"].ToString();
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
            
        }
    }

  • 相关阅读:
    C#将List<T>转化为DataTable
    SqlServer常用内置函数
    C#索引器
    验证Textbox的字符长度
    WM消息对应的Message消息中的Lparam和WParam
    对窗体操作的WM消息
    DllImport使用
    C#获取当前路径的七种方法
    注册ActiveX控件
    [转]VS2010中水晶报表安装应用及实例
  • 原文地址:https://www.cnblogs.com/huige1004/p/1378138.html
Copyright © 2011-2022 走看看