c# - gridview 排序不适用于数字

标签 c# asp.net gridview sorting

我已经在我的代码隐藏中实现了排序功能,它适用于单词但不适用于数字... 例如

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/

相关文章:

c# - 剑道下拉返回对象对象

c# - LINQ 失败了吗?

c# - 是否可以在一定天数后删除特定的 blob? Azure Blob 存储

c# - 如何将 google oauth 2.0 限制为仅在 ASP.NET Core Web 应用程序中的特定域?

asp.net - 使用用户时间在 GridView 中显示 DateTime

android - 删除在 gridView 中不可见的 View

c# - 在 COM Called Wrapper 中使用 .NET Generic List ToArray 会导致访问冲突,我是否遗漏了什么?

javascript - 在richtexteditor中播放上传视频的问题

asp.net - 模拟WCF

asp.net - 使用 FindControl 获取内容页中的 GridView