GridView的排序不工作的数 [英] gridview sort not working for numbers

查看:105
本文介绍了GridView的排序不工作的数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了某种功能在我的codebehind,它工作正常使用的话,但不能与数字...
例如:

  4,693
1,494
23

当我解决这我得到

 > 1,494
> 23
> 4,693

所以这意味着它只是检查的第一个号码......

我的code的排序是:

 保护无效GridView1_Sorting(对象发件人,GridViewSortEventArgs E)
    {
        如果(的IsPostBack)
        {
            DataTable的DT =会话[TaskTable]作为数据表;            如果(DT!= NULL)
            {                //排序的数据。
                dt.DefaultView.Sort = e.SortEx pression ++ GetSortDirection(e.SortEx pression);
                GridView1.DataSource =会话[TaskTable];
                GridView1.DataBind();
            }
        }
        其他
        {
            的Response.Redirect(〜/ Reports1mod.aspx);
        }    }    私人字符串GetSortDirection(字符串列)
    {
        //默认情况下,设置排序方向上升。
        字符串sortDirection =ASC;        //检索进行了排序最后一列。
        字符串SORTEX pression =的ViewState [SORTEX pression]作为字符串;        如果(SORTEX pression!= NULL)
        {
            //检查同一列进行排序。
            //否则,可以返回默认值。
            如果(SORTEX pression ==列)
            {
                字符串lastDirection =的ViewState [SortDirection]作为字符串;
                如果((lastDirection = NULL)及!及(lastDirection ==ASC))
                {
                    sortDirection =DESC;
                }
            }
        }        //在ViewState中保存新值。
        的ViewState [SortDirection] = sortDirection;
        的ViewState [SORTEX pression] =列;        返回sortDirection;
    }


解决方案

正如人们所说的,你有没有约束列一个字符串来获得这些逗号?

您应该让该列绑定到int值,并设置DataFormatString为{0:N}的数字与组分隔符。 (请参阅 BoundField.DataFormatString物业

I have implemented the sort function in my codebehind, it works fine with words but not with numbers... eg

4,693 
1,494  
23

when i sort this i get

> 1,494
> 23
> 4,693

so this means its just checking the first number....

my code for sort is:

 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;
    }

解决方案

As has been said, have you bound the column to a string to get those commas?

You should let the column bind to the int value, and set the DataFormatString to "{0:N}" for numeric with group separator. (See BoundField.DataFormatString Property)

这篇关于GridView的排序不工作的数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆