[英]Sorting a Gridview where columns are not automatically generated
我有一個DataTable,它從sql源獲取數據並填充gridview。 不能從sql語句進行排序,因為該順序對於執行某些初始表操作很重要。
我希望用戶能夠單擊一列,並讓gridview按該列中的值排序。 我已經在Google上搜索並找到了許多指南,但實際上沒有一個對我有用。 列永遠不可單擊。 我能夠用這段代碼實現可單擊的行,這些行可以執行其他操作,並且我想知道這是否會導致任何干擾,如果不能,那么為什么不能單擊以對行進行排序? 是因為我的列不是自動生成的嗎?
//對於可點擊的行
if (e.Row.RowType == DataControlRowType.DataRow) { GridViewRow row = e.Row; DataRow data = ((DataRowView)row.DataItem).Row; e.Row.Attributes.Add("onmouseover","this.style.backgroundColor='#ceedfc'"); if (data.Field<string>("rowType") != "total") e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''"); e.Row.Attributes.Add("style", "cursor:pointer;"); e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(suiteReport, "Select$" + e.Row.RowIndex);
我的排序嘗試是
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection) ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
protected void suiteReport_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression,string direction)
{
DataTable dt = (DataTable)Session["QueryTable"];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
suiteReport.DataSource = dv;
suiteReport.DataBind();
}
我基於此
但是,就像我說的那樣,這似乎對gridview沒有影響。 在aspx文件中,我的屬性“ AllowSorting”等於true,OnSorting調用suiteReport_Sorting。
我不確定問題出在哪里,因此任何幫助將不勝感激。
另外,如果有任何我不希望看到的代碼,請告訴我。
謝謝
編輯 :這是我處理回郵的方式
`if(!Page.IsPostBack){
try
{
cn1.Open();
cmd1 = new SqlCommand("sp_UpdateUsageLog", cn1);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add(new SqlParameter("@userName", SqlDbType.VarChar)).Value = userName;
cmd1.Parameters.Add(new SqlParameter("@userIpAddress", SqlDbType.VarChar)).Value = userIpAddress;
cmd1.Parameters.Add(new SqlParameter("@userComputerName", SqlDbType.VarChar)).Value = userComputerName;
cmd1.Parameters.Add(new SqlParameter("@pageViewed", SqlDbType.VarChar)).Value = "Store Report Card";
cmd1.Parameters.Add(new SqlParameter("@visitDate", SqlDbType.DateTime)).Value = DateTime.Now;
cmd1.ExecuteNonQuery();
}
catch { }
finally
{
cn1.Close();
}
EDIT2:這是gridview的aspx:
<asp:GridView ID="suiteReport" AutoGenerateColumns="false" ShowFooter="true" runat="server" OnRowDataBound="suiteReport_RowDataBound" OnSelectedIndexChanged="suiteReport_SelectedIndexChanged" AllowSorting="true" OnSorting="suiteReport_Sorting">
我相信我已經解決了,或者至少取得了足夠的進展。 我的標題行不可點擊的原因是因為我忽略了將SortExpressions添加到綁定字段中。 因為我是動態構建列的,所以我通過遍歷gridview中的標題,創建一個新的boundfield(這是我之前做過的)並給BoundField一個SortExpression(myBoundField.SortExpression =“ headerName”)來實現的。
希望這對任何也陷入困境的人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.