2009-12-18 63 views
1

我已经实现了某种功能我隐藏,它工作正常用语言,但不能与数字... 如GridView的排序不工作的数字

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

这可能是因为表中包含字符串,而不是整数。 – 2009-12-18 07:56:55

+0

您是如何填充字段的......您是否使用dataformat字符串填充为数字,或者将数字转换为字符串以添加逗号,然后填充Grid View列? – 2009-12-18 08:13:37

回答

2

如前所述,你是否将列绑定到一个字符串以获得这些逗号?

您应让列绑定到int值,并将DataFormatString的数值设置为“{0:N}”,并使用组分隔符。 (见BoundField.DataFormatString Property

2

数字排序为字符串时会出现这种情况。

它梳理串左到右,而你的情况之前。

1

它看起来像是将数字排序为字符串 - 即按字母顺序而不是数字顺序。我不太清楚你的实际列/值在哪里,你是否可以用某种类型的转换/转换为整数来包围它?