就是点击一列的标头就能升啊降啊排序的那种,如果数据源是dataset的话我大致也了解,就是把视图给排一下序,说白了就是对数据源操作。但我的问题是对象数组数据源,因为要用到三层架构啊,有数据实体对象,所以我就懵了。如果只是对一个字段进行排序还好,只要继承一下IComparable接口,用数据对象sort一下也好办,问题是有好多个字段都要可排序T_T等待高手救援……
解决方案 »
- 母板页的简单问题。。。。。。。!!!!!!!!!!!!!
- 谁有好点的aspx 用户管理模块分享?
- 关于customErrors的问题
- 客户端的某个功能每天只执行一次?请问实现思路,谢谢。。
- 服务器报错,请问这是什么错误啊?
- 在vs2008中没有错误,在IIS7中出现编译错误
- 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败
- 请问网站 后台代码 不写 try catch final 可以吗?什么时候该用?
- 在一个页面中如何显示多张图片???
- 如何实现这样的功能
- JS中getElementById实例化总是为空
- div 里面放iframe页面下拉被隐藏的问题
用grid的sorting
事件值得数据列时候舍得排序字段名
using System;
using System.Collections.Generic;public class DinoComparer: IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length); if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
}public class Example
{
public static void Main()
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Pachycephalosaurus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Mamenchisaurus");
dinosaurs.Add("Deinonychus");
Display(dinosaurs); DinoComparer dc = new DinoComparer(); Console.WriteLine("\nSort with alternate comparer:");
dinosaurs.Sort(dc);
Display(dinosaurs); SearchAndInsert(dinosaurs, "Coelophysis", dc);
Display(dinosaurs); SearchAndInsert(dinosaurs, "Oviraptor", dc);
Display(dinosaurs); SearchAndInsert(dinosaurs, "Tyrannosaur", dc);
Display(dinosaurs); SearchAndInsert(dinosaurs, null, dc);
Display(dinosaurs);
} private static void SearchAndInsert(List<string> list,
string insert, DinoComparer dc)
{
Console.WriteLine("\nBinarySearch and Insert \"{0}\":", insert); int index = list.BinarySearch(insert, dc); if (index < 0)
{
list.Insert(~index, insert);
}
} private static void Display(List<string> list)
{
Console.WriteLine();
foreach( string s in list )
{
Console.WriteLine(s);
}
}
}
public class NameCompare :ICompare<Student>
{
public int Compare(Student x,Student y)
{
return (x.name.CompareTo(y.name));
}
}Students.Sort(new NameCompare);
期待高手了......
using System;
using System.Collections.Generic;
using System.Text;namespace 排序使用委托示例
{
delegate bool Compar(object lhs,object rhs);
class Program
{
static void Main(string[] args)
{
Compar getMethod = new Compar(Employees.compareSalary);//注意不仅可以比工资,还能比名字
Employees[] employees = new Employees[] {
new Employees("shaka",2000),
new Employees("jack",1500),
new Employees("Rose",1200),
new Employees("Ruby",2400),
new Employees("Booal",3000)
};
Sort.sort(employees, getMethod);
for (int i = 0; i < employees.Length; i++)
{
Console.WriteLine(employees[i].ToString());
}
}
}
class Employees
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
} private decimal salary;
public decimal Salary
{
get { return salary; }
set { salary = value; }
} public Employees(string name, decimal salary)
{
this.name = name;
this.salary = salary;
} public static bool compareSalary(object lhs, object rhs)
{
Employees elhs = (Employees)lhs;
Employees erhs = (Employees)rhs;
return (elhs.Salary > erhs.Salary);
}
public static bool compareName(object lhs, object rhs)
{
Employees elhs = (Employees)lhs;
Employees erhs = (Employees)rhs;
return (elhs.Name.Equals(erhs.Name));
}
public override string ToString()
{
return string.Format(name + ",{0:c}", salary);
}
}
class Sort
{
public static void sort(object[] sortArray, Compar getMethod)
{
for (int i = 0; i < sortArray.Length; i++)
{
for (int j = i + 1; j < sortArray.Length; j++)
{
if (getMethod(sortArray[j], sortArray[i]))
{
object temp = sortArray[i];
sortArray[i] = sortArray[j];
sortArray[j] = temp;
}
}
}
}
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
onselecting="LinqDataSource1_Selecting" >
</asp:LinqDataSource>
</div>
<asp:GridView ID="GridView1" runat="server"
DataSourceID="LinqDataSource1" AllowSorting="True">
</asp:GridView>
</form>
</body>
</html>using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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;
using System.Xml.Linq;public partial class _Default : System.Web.UI.Page
{
Stutent[] students;
protected void Page_Load(object sender, EventArgs e)
{
students = new Stutent[]{
new Stutent("shaka",22,"男"),
new Stutent("shaka",12,"男"),
new Stutent("romeo",34,"男"),
new Stutent("rose",45,"女"),
new Stutent("ruby",67,"女"),
new Stutent("kevin",23,"男"),
new Stutent("jordan",22,"男")
};
}
protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
e.Result = students;
}
}public class Stutent
{
public Stutent(string name, int age, string sex)
{
this.name = name;
this.age = age;
this.sex = sex;
}
private int age; public int Age
{
get { return age; }
set { age = value; }
}
private string sex; public string Sex
{
get { return sex; }
set { sex = value; }
}
private string name; public string Name
{
get { return name; }
set { name = value; }
}
}