Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# gridview排序不适用于数字_C#_Asp.net_Gridview_Sorting - Fatal编程技术网

C# gridview排序不适用于数字

C# gridview排序不适用于数字,c#,asp.net,gridview,sorting,C#,Asp.net,Gridview,Sorting,我已经在我的codebehind中实现了排序功能,它可以很好地处理单词,但不能处理数字。。。 乙二醇 当我整理这个时,我得到了 > 1,494 > 23 > 4,693 这意味着它只是检查第一个数字 我的排序代码是: protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { if (IsPostBack) { DataT

我已经在我的codebehind中实现了排序功能,它可以很好地处理单词,但不能处理数字。。。 乙二醇

当我整理这个时,我得到了

> 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;
    }
将数字排序为字符串时会发生这种情况


它将字符串从左到右排序,在您的例子中,23中的2在4之前

看起来它将数字排序为字符串,即按字母顺序而不是数字顺序。我不太清楚您的实际列/值在哪里,您能用某种类型的强制转换/转换为整数来围绕它吗?

如前所述,您是否将列绑定到字符串以获得那些逗号


您应该让列绑定到int值,并将带有组分隔符的numeric的DataFormatString设置为“{0:N}”。(请参阅)

在参考MSDN页面快速教程后,我也遇到了这个问题:

您只需使用第二个参数指定TableData列的类型:

   //To allow sorting numerically, add type param to column
   table1.Columns.Add("GridTest ID", typeof(Int32));

希望这对其他人有帮助。

可能是因为表中包含字符串,而不是整数。您是如何填充字段的。。。您是否使用dataformat字符串填充为数字,或者将数字转换为字符串以添加逗号,然后填充网格视图列?
   //To allow sorting numerically, add type param to column
   table1.Columns.Add("GridTest ID", typeof(Int32));