好吧我想通了。以下是解決方案:
- 將BLL綁定到ObjectDataSource。
- 爲BLL中的select方法提供重載方法,以支持分頁和排序。
- 在ObjectDataSource中提供SortParameterName。這是BLL中選擇方法的字符串輸入參數的名稱。
欲瞭解更多信息,請參閱: http://msdn.microsoft.com/en-us/library/aa479347.aspx
下面是一個例子,這僅僅是演示中,我不支持排序方向,或優化代碼等一個quck例如:
namespace CodeSamples.DAL
{
public static class DAL
{
public static CustomerList GetCustomerList(string SortExpression)
{
return GetCustomerList(int.MaxValue, 0, SortExpression);
}
public static CustomerList GetCustomerList()
{
return GetCustomerList(int.MaxValue, 0,String.Empty);
}
public static CustomerList GetCustomerList(int maximumRows, int startRowIndex, string SortExpression)
{
const string query = "Select * from Customers";
CustomerList customers = new CustomerList();
SqlConnection conn = new SqlConnection("Data Source=Win2k8;Initial Catalog=NorthWind;User ID=sa;Password=XXXXX");
SqlCommand command = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = command.ExecuteReader();
ArrayList rows = new ArrayList();
while (reader.Read())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
rows.Add(values);
}
conn.Close();
int currentIndex = 0;
int itemsRead = 0;
int totalRecords = rows.Count;
foreach (object[] row in rows)
{
if (currentIndex >= startRowIndex && itemsRead <= maximumRows)
{
customers.Add(new Customer { Name = row[1].ToString(), ID = row[0].ToString(), ContactName = row[2].ToString() });
itemsRead++;
}
currentIndex++;
}
CustomerList sortedCustomers = new CustomerList();
string sortBy = SortExpression;
bool isDescending = false;
if (SortExpression.ToLowerInvariant().EndsWith(" desc"))
{
sortBy = SortExpression.Substring(0, SortExpression.Length - 5);
isDescending = true;
}
var sortedList = from customer in customers
select customer;
switch (sortBy)
{
case "ID":
sortedList = isDescending ? sortedList.OrderByDescending(cust => cust.ID) : sortedList.OrderBy(cust => cust.ID);
break;
case "Name":
sortedList = isDescending ? sortedList.OrderByDescending(cust => cust.Name) : sortedList.OrderBy(cust => cust.Name);
break;
case "ContactName":
sortedList = isDescending ? sortedList.OrderByDescending(cust => cust.ContactName) : sortedList.OrderBy(cust => cust.ContactName);
break;
}
foreach (Customer x in sortedList)
{
sortedCustomers.Add(x);
}
return sortedCustomers;
}
}
public class CustomerList : List<Customer>
{
}
public class Customer
{
public Customer()
{
}
public Customer(string Name, string id)
{
this.Name = Name;
ID = id;
}
public string ID
{
get;
set;
}
public string Name
{
get;
set;
}
public string ContactName
{
get;
set;
}
}
}
在ASPX頁面:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"
AllowSorting="True">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetCustomerList" SortParameterName="SortExpression"
TypeName="CodeSamples.DAL.DAL">
</asp:ObjectDataSource>
欲瞭解更多信息,請參閱: http://msdn.microsoft.com/en-us/library/aa479347.aspx
來源
2010-03-31 19:53:59
ace
大量的有關在這裏排序GridView的好信息:http://msdn.microsoft.com/en-us/library/hwf94875(VS.80).aspx – 2010-03-31 02:18:22