• asp.net DataGrid排序


    前台代码:

    View Code
    <%@ Register Assembly="WY.Web.Common" Namespace="WY.Web.Common.WebControls" TagPrefix="cc1" %>
     <cc1:DataGrid ID="DataGrid1" runat="server"  AllowCustomPaging="True" 
                    onitemdatabound="DataGrid1_ItemDataBound">
                <Columns>                                  
                     <asp:TemplateColumn SortExpression="Date" HeaderText="日期" ItemStyle-Width="140">
                        <ItemTemplate>
                           <a href="javascript:void(0)" onclick="doAdd('workrecordedit.aspx?id=<%#Utils.NvStr(Eval("Date")) %>','填写日报',980,560)" ><%#Utils.NvStr(Eval("Date")) %></a>
                        </ItemTemplate> 
                    </asp:TemplateColumn>         
                    <asp:BoundColumn DataField="Type" SortExpression="Type" HeaderText="日期种类" ItemStyle-Width = "140"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Content" SortExpression="Content" HeaderText="工作内容"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Record" SortExpression="Record" HeaderText="考勤记录" ItemStyle-Width="200"></asp:BoundColumn>                                
               </Columns>
            </cc1:DataGrid>

    CS:

    View Code
      protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
                InitializeComponent();
            }
    
            private void InitializeComponent()
            {
                this.DataGrid1.SortCommand += new DataGridSortCommandEventHandler(DataGrid_SortCommand);
                this.DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid_PageIndexChanged);
                this.DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
    
                this.DataGrid1.AllowCustomPaging = true;
                this.DataGrid1.AllowSorting = true;
                this.DataGrid1.SaveDSViewState = true;
            }
    
            public void GoToPagerButton_Click(object sender, EventArgs e)
            {
                try
                {
                    BindData();
                }
                catch { }
            }
    
            public void DataGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
            {
                try
                {
                    ((WY.Web.Common.WebControls.DataGrid)source).CurrentPageIndex = e.NewPageIndex;
                    BindData();
                }
                catch { }
            }
    
            protected void DataGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
            {
                ((WY.Web.Common.WebControls.DataGrid)source).Sort = e.SortExpression.ToString();
                BindData();
            }
    View Code
     DataView dv = getNewDatatable(dtb, j, dts, DataGrid1).DefaultView;
                if (!string.IsNullOrEmpty(((WY.Web.Common.WebControls.DataGrid)DataGrid1).Sort))
                {
                   // dv.Sort = ((WY.Web.Common.WebControls.DataGrid)DataGrid1).Sort + " " + DataGrid1.DataGridSortType;
                    dv.Sort = ((WY.Web.Common.WebControls.DataGrid)DataGrid1).Sort + " " + DataGrid1.GetDataGridSortType();
                }
                DataGrid1.DataSource = dv;
                DataGrid1.DataBind();
    View Code
     public string GetDataGridSortType()
            {
                object obj = ViewState["DataGridSortType"];
                string ascordesc = obj == null ? "ASC" : (string)obj;
                return ascordesc == "ASC" ? "DESC" : "ASC";
            }
  • 相关阅读:
    UIWindow与UIView
    UIView与CALayer 区别
    setter getter 方法
    KVC、KVO 理解
    c语言实现单链表
    浅谈C的应用与常见error
    POJ 3683 Priest John's Busiest Day(2-SAT+方案输出)
    Google Code Jam 2008 Round 1A C Numbers(矩阵快速幂+化简方程,好题)
    POJ 3686 The Windy's(思维+费用流好题)
    POJ 2686 Traveling by Stagecoach(状压二维SPFA)
  • 原文地址:https://www.cnblogs.com/haoxr/p/3054296.html
Copyright © 2020-2023  润新知