using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using BiscomFax;
namespace FaxServer
{
public partial class _Default : System.Web.UI.Page
{
public const string vsColumn = "Column";
public const string vsSortDirection = "SortDirection";
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
ViewState[vsColumn] = "";
List_Click(ActivityButton, e);
}
protected void List_Click(object sender, EventArgs e)
{
//using (new Impersonator("administrator", "mlabs.com", "100%secure*"))
//{
try
{
Fax fax = new Fax();
ConnObj cnObj = GetConfiguration();
Button btn = (Button)sender;
string sort = "";
DataTable dt = new DataTable();
switch (btn.CommandName)
{
case "Activity":
sort = "DateTime";
dt = fax.GetActivityLog(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
break;
case "Message":
sort = "DateTime";
dt = fax.GetMessageStatus(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
break;
case "Pending":
sort = "DeliveryTime";
dt = fax.GetPendingList(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
break;
default:
sort = "DateTime";
dt = fax.GetActivityLog(cnObj.faxDir, cnObj.faxUsername, cnObj.faxPassword);
break;
}
GridView1.DataSource = dt;
GridView1.Sort(sort, SortDirection.Descending);
GridView1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
//}
}
protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = GridView1.DataSource as DataTable;
if (dt != null)
{
DataView dv = new DataView(dt);
string oldSort = ViewState[vsColumn].ToString();
dv.Sort = e.SortExpression + " " + convertSorDirectionToSql(e.SortDirection);
if (dv.Sort == oldSort)
dv.Sort = e.SortExpression + " " + convertSorDirectionToSql(SortDirection.Descending);
ViewState[vsColumn] = dv.Sort;
GridView1.DataSource = dv;
GridView1.DataBind();
}
}
我有一個非常困難的時間排序這個gridview的內容,我知道我綁定正確,因爲數據顯示,但數據不會按照日期時間排序。我做錯了什麼?c#asp.net gridview不排序
當我做了排序像在此之前我一直沒能在回發利用GridView的DataSource屬性。我不得不再次從底層數據源獲取數據。您是否嘗試過將「dt」分配給數據集的新副本? – CheckRaise 2011-05-26 18:03:26
我們可以確定問題的唯一方法是,您需要發佈網格視圖的aspx內容 - 尤其是,日期列的模板。 – Hogan 2011-05-26 19:42:30
@hogan別的? – 2011-05-26 19:57:00