【问题标题】:can't sort data int properly with gridview无法使用 gridview 正确排序数据 int
【发布时间】:2013-06-01 17:59:57
【问题描述】:

我想对我的整数数据进行排序,但我想让它更易于阅读,如果我有像 1000000000 这样的数据,我希望它显示 1,000,000,000 所以我在 mysql 中使用这个查询;

format(col_name,0)

我尝试在 C# 中使用 gridview 使用 sort 函数对其进行排序,我使用它对 gridview 进行排序;

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
    tempExp = e.SortExpression;
    Session["sort"] = tempExp;
    showData();
}
void showData()
{
    tempExp = (string)Session["sort"];
    sortProperty = SortDirection.Descending;
    sortedView = new DataView(dataset); 
    sortedView.Sort = tempExp + " Desc"; 
    GridView1.DataSource = sortedView; 
    GridView1.DataBind();
}

但这就是我尝试对 data2 进行排序时发生的情况;

+================+=================+
|     data1      |      data2      |
+================+=================+
|     21,039,000 |               6 |
|     30,080,000 |           4,062 |
|    209,120,040 |          28,692 |
|    201,200,900 |           2,115 |
|      1,100,900 |          15,858 |
+================+=================+

我该如何解决?

【问题讨论】:

  • 如果你不应用排序功能,你怎么能期望它被排序?
  • 一般来说,我不会在 SQL 查询中应用数字格式。这就是用户界面的用途。有一个How to: Format Data in the Windows Forms DataGridView Control 可能会对您有所帮助。
  • 为什么这个标签是 MySQL?如果您可以在 SQL 中进行排序,请显示查询。如果没有,请删除标签。
  • 我阅读了How to: Format Data in the Windows Forms DataGridView Control 并使用了this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c"; 我已经添加了namspace System.Windows.Forms 并添加了引用System.Windows.Forms.dll 但仍然找不到DefaultCellStyle
  • 为什么不在 mySQL 中对其进行排序,然后再将其传递给 DataGridView?

标签: c# sorting gridview


【解决方案1】:

第一个解决方案:

在 C# 代码中进行格式化。

int num = 11111111;
string s = num.ToString("N0");

第二种解决方案:

在 sql 查询中包含原始 int 列以及格式化值并对原始 int 列应用排序并在 gridview 中绑定格式化列以进行显示。

【讨论】:

    【解决方案2】:

    一个解决方案可能是在您的 mySQL 查询中,您将同时拥有 Data1 和 Data2 的格式化版本和未格式化版本,然后根据未格式化版本对其进行排序,因此您的查询将如下所示:

    SELECT Data1 as A, format(Data1,0) as 'Data1', 
    Data2 as B, format(Data2,0) as 'Data2' 
    FROM `tabletest`
    

    然后,如果用户单击以使用Data1 进行排序,那么您将使用'A'(上面Data1 的别名)而不是Data1(格式化版本)对其进行排序,但如果使用Data2 进行排序,则使用'B' (再次作为别名)。因此,您的代码将如下所示:

    void showData()
    {
     tempExp = (string)Session["sort"];
     sortProperty = SortDirection.Descending;
     sortedView = new DataView(dataset);
     String newSort = tempExp == 'Data1' ? 'A' : 'B'; // Use the Alias to sort 
     sortedView.Sort = newSort + " Desc"; 
     GridView1.DataSource = sortedView;
     GridView1.Columns['A'].Visible = false; // Hide sorting column A to the user
     GridView1.Columns['B'].Visible = false; // Hide sorting column B to the user
     GridView1.DataBind();
    }
    

    因此,如果您在那里观察到,我首先检查用户想要排序的列并将其切换到非格式化的排序器列,即“A”(对于Data1)或“B”(对于@987654331 @) 喜欢:

    String newSort = tempExp == 'Data1' > 'A' : 'B'; // Use the Alias to sort 
    sortedView.Sort = newSort + " Desc"; 
    

    然后将 Sorter 列从用户的视线中隐藏起来,但一定要在将其分配给 DataGridView 的 DataSource 之后将其放置,否则您一开始就没有列可言。

    GridView1.DataSource = sortedView;
    GridView1.Columns['A'].Visible = false; // Hide sorting column A aka Data1 to the user
    GridView1.Columns['B'].Visible = false; // Hide sorting column B aka Data2 to the user
    

    【讨论】:

      猜你喜欢
      • 2013-05-27
      • 2022-10-24
      • 2012-12-31
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      相关资源
      最近更新 更多