【发布时间】:2014-06-06 17:06:53
【问题描述】:
我有一个从 sql 源获取数据并填充 gridview 的 DataTable。从 sql 语句排序不是一个选项,因为顺序对于执行一些初始表操作很重要。
我希望用户能够单击一列并让 gridview 按该列中的值排序。我已经用谷歌搜索并找到了很多指南,但没有一个真正适合我。列永远不可点击。我能够使用这段代码实现执行其他操作的可点击行,我想知道这是否会导致任何干扰,如果没有,为什么我不能点击对行进行排序?是不是因为我的列没有自动生成?
//对于可点击的行
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();
}
我基于this
但是,就像我说的,这似乎对 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">
【问题讨论】:
-
oyu 是否正确捕获回发事件并且不让页面重新写入?
-
@Dean.DePue 我确实对 Page.IsPostBack 有意见,但是“不让页面重新编写”是什么意思?
-
回发时你会怎么做?网格中是否出现完全相同的排序?
-
@Dean.DePue 我相信是的,是的。单击各种按钮只会更改显示哪些行,但数据表保持不变且顺序相同。
-
回发时的代码是什么样的?
标签: c# asp.net sorting gridview