0
發生變化我使用分頁將GridView綁定到ObjectDataSource。分頁工作正常,除了排序順序根據查看結果的哪個頁面而變化。這導致項目在其他問題中重新出現在後續頁面上。我將問題追溯到我的DAL,它一次讀取一頁,然後對其進行分類。很明顯,排序會隨着結果集大小的改變而改變。這個算法有改進嗎?如果可能,我想使用數據讀取器:問題:對GridView/ObjectDataSource進行排序根據頁面
[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
public static WordsCollection LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression)
{
//DEFAULT SORT EXPRESSION
if (string.IsNullOrEmpty(sortExpression)) sortExpression = "OrderBy";
//CREATE THE DYNAMIC SQL TO LOAD OBJECT
StringBuilder selectQuery = new StringBuilder();
selectQuery.Append("SELECT");
if (maximumRows > 0) selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString());
selectQuery.Append(" " + Words.GetColumnNames(string.Empty));
selectQuery.Append(" FROM sw_Words");
string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " WHERE " + sqlCriteria;
selectQuery.Append(whereClause);
selectQuery.Append(" ORDER BY " + sortExpression);
Database database = Token.Instance.Database;
DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString());
//EXECUTE THE COMMAND
WordsCollection results = new WordsCollection();
int thisIndex = 0;
int rowCount = 0;
using (IDataReader dr = database.ExecuteReader(selectCommand))
{
while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows)))
{
if (thisIndex >= startRowIndex)
{
Words varWords = new Words();
Words.LoadDataReader(varWords, dr);
results.Add(varWords);
rowCount++;
}
thisIndex++;
}
dr.Close();
}
return results;
}