[英]c# asp.net gridview not sorting
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();
}
}
i am having a very difficult time sorting the contents of this gridview, i know that i am binding correctly becuase the data is showing but the data does not get sorted at all by DateTime.我很难对这个 gridview 的内容进行排序,我知道我正在正确绑定,因为数据正在显示,但数据根本没有按 DateTime 排序。 what am i doing wrongly?
我做错了什么?
Lets look at this line让我们看看这条线
dv.Sort = e.SortExpression + " " + convertSorDirectionToSql(e.SortDirection);
While testing, when you put a breakpoint on this line what value did e.SortExpression
have?在测试时,当你在这一行设置断点时,
e.SortExpression
有什么值?
Your databind is correct.您的数据绑定是正确的。 here is a nice article for sorting in gridview: http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1418
这是一篇用于在 gridview 中排序的好文章: http://www.mikepope.com/blog/DisplayBlog.aspx?permalink=1418
[update] [更新]
Your code has several inconsistency.您的代码有几个不一致之处。 You probably need to implement it again.
您可能需要再次实施它。
here is a good example in C# : http://programming.top54u.com/post/ASP-Net-2-0-Gridview-Sorting-Using-C-sharp.aspx这是 C# 中的一个很好的例子: http ://programming.top54u.com/post/ASP-Net-2-0-Gridview-Sorting-Using-C-sharp.aspx
You could sort the datatable by setting it's property datatable.defaultview.sort
.您可以通过设置属性
datatable.defaultview.sort
对数据表进行排序。 Don't know of the top of my head whether you need to bind the datatable or the datatable.defaultview
after that though.不知道您是否需要在此之后绑定数据表或
datatable.defaultview
。
Try This code试试这个代码
protected void grdList1_Sorting(object sender, GridViewSortEventArgs e)
{
fillgrid();
string sortstr = e.SortExpression;
DataView dview = new DataView(dtable);
if (sortstr == "asc")
dview.Sort = e.SortExpression + " desc";
else
dview.Sort = e.SortExpression + " asc";
grdList1.DataSource = dview;
grdList1.DataBind();
}
just check allowsorting property on aspx page in gridview control只需检查 gridview 控件中 aspx 页面上的alloworting属性
It may be false.这可能是假的。
please make it true and then check sorting.请使其正确,然后检查排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.