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";
        }

 

posted @ 2013-05-02 11:22  菱花淚硃砂  阅读(231)  评论(0编辑  收藏  举报