我已经在我的代码隐藏中实现了排序功能,它适用于单词但不适用于数字... 例如
4,693
1,494
23
当我排序时我得到
> 1,494
> 23
> 4,693
所以这意味着它只是检查第一个数字....
我的排序代码是:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (IsPostBack)
{
DataTable dt = Session["TaskTable"] as DataTable;
if (dt != null)
{
//Sort the data.
dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
GridView1.DataSource = Session["TaskTable"];
GridView1.DataBind();
}
}
else
{
Response.Redirect("~/Reports1mod.aspx");
}
}
private string GetSortDirection(string column)
{
// By default, set the sort direction to ascending.
string sortDirection = "ASC";
// Retrieve the last column that was sorted.
string sortExpression = ViewState["SortExpression"] as string;
if (sortExpression != null)
{
// Check if the same column is being sorted.
// Otherwise, the default value can be returned.
if (sortExpression == column)
{
string lastDirection = ViewState["SortDirection"] as string;
if ((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
// Save new values in ViewState.
ViewState["SortDirection"] = sortDirection;
ViewState["SortExpression"] = column;
return sortDirection;
}
最佳答案
如前所述,您是否已将列绑定(bind)到字符串以获取这些逗号?
您应该让列绑定(bind)到 int 值,并将 DataFormatString 设置为“{0:N}”(对于带组分隔符的数字)。 (参见 BoundField.DataFormatString Property)
关于c# - gridview 排序不适用于数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1926912/