C#中GridView的排序用法

 

 

此方法摘录于:

http://topic.csdn.net/u/20071109/20/C598BB6E-4CA8-4CD0-8204-E42A315FD5D0.html

1、利用DataView来进行排序。 

使用Seesion把排序的状态值保存,再利用DataView的Sort方法进行排序,再次帮带到GridView的数据源上。

具体代码如下:

 

using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;

namespace  Orid.BPO.ResultAnalysis.Analysis
{
    
public partial class WebForm3 : System.Web.UI.Page
    
{
        DataTable dataTable;
        
protected void Page_Load(object sender, EventArgs e)
        
{
            dataTable 
= this.MakeCustomerTable();
            gridViewPublishers.DataSource 
= dataTable;
            gridViewPublishers.DataBind();
        }


        
//记录排序方向(升序/降序)
        private string GridViewSortDirection
        
{
            
get return ViewState["SortDirection"as string ?? "ASC"; }
            
set { ViewState["SortDirection"= value; }
        }

        
//记录排序字段
        private string GridViewSortExpression
        
{
            
get return ViewState["SortExpression"as string ?? string.Empty; }
            
set { ViewState["SortExpression"= value; }
        }

        
//取得排序方向(原本升序点了列头后降序排列)
        private string GetSortDirection()
        
{
            
switch (GridViewSortDirection)
            
{
                
case "ASC":
                    GridViewSortDirection 
= "DESC";
                    
break;
                
case "DESC":
                    GridViewSortDirection 
= "ASC";
                    
break;
            }

            
return GridViewSortDirection;
        }

        
//排序表
        protected DataView SortDataTable(DataTable dataTable)
        
{
            
if (dataTable != null)
            
{
                DataView dataView 
= new DataView(dataTable);
                
if (GridViewSortExpression != string.Empty)
                
{
                    dataView.Sort 
= string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
                }

                
return dataView;
            }

            
else
            
{
                
return new DataView();
            }

        }

        
//GridView的Sorting事件代理给的方法
        protected void gridViewPublishers_Sorting(object sender, GridViewSortEventArgs e)
        
{
            GridViewSortExpression 
= e.SortExpression;
            
int pageIndex = gridViewPublishers.PageIndex;
            gridViewPublishers.DataSource 
= SortDataTable(dataTable);
            gridViewPublishers.DataBind();
            gridViewPublishers.PageIndex 
= pageIndex;
        }


        
private DataTable MakeCustomerTable()
        
{
            
// Create a new DataTable.
            System.Data.DataTable table = new DataTable("ParentTable");
            
// Declare variables for DataColumn and DataRow objects.
            DataColumn column;
            DataRow row;

            
// Create new DataColumn, set DataType, 
            
// ColumnName and add to DataTable.    
            column = new DataColumn();
            column.DataType 
= System.Type.GetType("System.Int32");
            column.ColumnName 
= "ID";
            column.ReadOnly 
= true;
            column.Unique 
= true;
            
// Add the Column to the DataColumnCollection.
            table.Columns.Add(column);

            
// Create second column.
            column = new DataColumn();
            column.DataType 
= System.Type.GetType("System.String");
            column.ColumnName 
= "Name";
            column.AutoIncrement 
= false;
            column.ReadOnly 
= false;
            column.Unique 
= false;
            
// Add the column to the table.
            table.Columns.Add(column);

            
// Make the ID column the primary key column.
            
//DataColumn[] PrimaryKeyColumns = new DataColumn[1];
            
//PrimaryKeyColumns[0] = table.Columns["CustomerId"];
            
//table.PrimaryKey = PrimaryKeyColumns;
            
            
//Create three new DataRow objects and add 
            
//them to the DataTable
            for (int i = 0; i <= 2; i++)
            
{
                row 
= table.NewRow();
                row[
"ID"= i;
                row[
"Name"= "CustomerName " + i;
                table.Rows.Add(row);
            }

            
//table.Rows.Add(table.NewRow()); 
            return table;
        }


    }

}

 

在Html的定义GridView标签如下:

 

<% @ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="Orid.BPO.ResultAnalysis.Analysis.WebForm3"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head  runat ="server" >
    
< title > 无标题页 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
    
</ div >
    
< asp:GridView  ID ="gridViewPublishers"  AllowSorting ="true"  OnSorting ="gridViewPublishers_Sorting"
        runat
="server" >
        
< Columns >
            
< asp:BoundField  DataField ="ID"  HeaderText ="ID"  SortExpression ="ID"   />
            
< asp:BoundField  DataField ="Name"  HeaderText ="Name"  SortExpression ="Name"   />
        
</ Columns >
    
</ asp:GridView >
    
</ form >
</ body >
</ html >

 

上面就可以实现GridView的排序功能。可能各位都已经了解和用过此方法了,小弟献丑了,见笑见笑!

(但还是希望blog中收集的资料能对初学者有帮助)

希望那位能提供GridView本身内置的排序方法。。。期待。。。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值