求代码。

解决方案 »

  1.   

    在.aspx页面GridView绑定的表头中,加入“SortExpression”属性,一般是:view plaincopy to clipboardprint?
    <asp:BoundField DataField="totalnum" HeaderText="总数" SortExpression="totalnum"  />  
    <asp:BoundField DataField="totalnum" HeaderText="总数" SortExpression="totalnum"  /> 如果有模板列的话,也是一样的:view plaincopy to clipboardprint?
    <asp:TemplateField HeaderText="总大小(M)"  SortExpression="totalfilesize" >   
                                                     
                                                    <ItemTemplate>   
                                                        <asp:Label ID="Label1" runat="server" ></asp:Label>   
                                                    </ItemTemplate>   
                                                </asp:TemplateField>  
    <asp:TemplateField HeaderText="总大小(M)"  SortExpression="totalfilesize" >
                                                  
                                                    <ItemTemplate>
                                                        <asp:Label ID="Label1" runat="server" ></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField> 接下来是在.CS中写代码了:view plaincopy to clipboardprint?
    protected void Page_Load(object sender, EventArgs e)   
           {   
                        if(!Page.IsPostBack)   
             {   
                 this.GridView1.Attributes.Add("SortExpression", "totalnum");   
                 this.GridView1.Attributes.Add("SortDirection", "ASC");   
      
                 Bind();//自己写的GridView绑定方法   
             }   
                 
           }   
      
    public void Bind()   
           {   
               string sortExpression = this.GVRSType.Attributes["SortExpression"];   
               string sortDirection = this.GVRSType.Attributes["SortDirection"];   
      
                 
               DataTable dtBind = 要绑定的数据;   
      
               this.lblOrg.Text = "(" + ro.Name + ")";   
               if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))   
               {   
                   dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);   
               }   
      
               GridView1.DataSource = dtBind;   
               GridView1.DataBind();   
           }   
      
    protected void GVRSType_Sorting(object sender, GridViewSortEventArgs e)   
           {   
              string sortExpression = e.SortExpression.ToString();   
      
               // 假定为排序方向为“顺序”   
               string sortDirection = "ASC";   
      
               // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改   
               if (sortExpression == this.GridView1.Attributes["SortExpression"])   
               {   
                   //获得下一次的排序状态   
                   sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");   
               }   
      
               // 重新设定GridView排序数据列及排序方向   
               this.GridView1.Attributes["SortExpression"] = sortExpression;   
               this.GridView1.Attributes["SortDirection"] = sortDirection;   
      
               Bind();   
           }  
     protected void Page_Load(object sender, EventArgs e)
            {
                         if(!Page.IsPostBack)
              {
                  this.GridView1.Attributes.Add("SortExpression", "totalnum");
                  this.GridView1.Attributes.Add("SortDirection", "ASC");              Bind();//自己写的GridView绑定方法
              }
               
            } public void Bind()
            {
                string sortExpression = this.GVRSType.Attributes["SortExpression"];
                string sortDirection = this.GVRSType.Attributes["SortDirection"];           
                DataTable dtBind = 要绑定的数据;            this.lblOrg.Text = "(" + ro.Name + ")";
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }            GridView1.DataSource = dtBind;
                GridView1.DataBind();
            } protected void GVRSType_Sorting(object sender, GridViewSortEventArgs e)
            {
               string sortExpression = e.SortExpression.ToString();            // 假定为排序方向为“顺序”
                string sortDirection = "ASC";            // “ASC”与事件参数获取到的排序方向进行比较,进行GridView排序方向参数的修改
                if (sortExpression == this.GridView1.Attributes["SortExpression"])
                {
                    //获得下一次的排序状态
                    sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
                }            // 重新设定GridView排序数据列及排序方向
                this.GridView1.Attributes["SortExpression"] = sortExpression;
                this.GridView1.Attributes["SortDirection"] = sortDirection;            Bind();
            } 有时间看看怎么加个按钮吧!
    http://blog.csdn.net/fenyuxiao/archive/2009/04/20/4094352.aspx
      

  2.   

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="GridView1_Sorting" AllowPaging="true" PageSize="10" OnPageIndexChanging="GridView1_PageIndexChanging">  
      <Columns>  
      <asp:BoundField DataField="id" HeaderText="代码" SortExpression="id"/>  
      <asp:BoundField DataField="name" HeaderText="名称" SortExpression="name"/>  
      <asp:TemplateField SortExpression="BUID" HeaderText="BUID">  
      <ItemTemplate>  
      <asp:Label ID="Label1" runat="server" Text=' <%# Bind("ID") %>' ToolTip=' <%# DataBinder.Eval(Container.DataItem, "ID") %>'> </asp:Label>  
      </ItemTemplate>  
      </asp:TemplateField>  
      </Columns>  
      </asp:GridView>  protected void Page_Load(object sender, EventArgs e)  
      {  
      if (!this.IsPostBack)  
      {  
      ViewState["SortOrder"] = "deptid";  
      ViewState["OrderDire"] = "asc";  
      BindGridView();  
      }  
      }    protected void BindGridView()  
      {  
        
      DataSet ds = new DataSet();  
          DataView dv = ds.Tables[0].DefaultView;  
      string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];  
      dv.Sort = sort;  
      this.GridView1.DataSource = dv;    this.GridView1.DataBind();    }  
      protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)  
      {  
      string vortExp = e.SortExpression;  
      if (ViewState["SortOrder"].ToString() == vortExp)  
      {  
      if ((string)ViewState["OrderDire"] == "desc")  
      {  
      ViewState["OrderDire"] = "asc";  
      }  
      else if ((string)ViewState["OrderDire"] == "asc")  
      {  
      ViewState["OrderDire"] = "desc";  
      }  
      }  
      else  
      {  
      ViewState["SortOrder"] = e.SortExpression;  
      }  
      BindGridView();    }  
      protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)  
      {  
      this.GridView1.PageIndex = e.NewPageIndex;  
      BindGridView();  
      }  
      

  3.   

    正解,这是自定义绑订,当然还可以与SQLDATASOURC和OBJECTDATASOURCE绑订