不能在gridview中正确排序数据
本文关键字:排序 数据 gridview 不能 | 更新日期: 2023-09-27 18:16:26
我想排序我的整数数据,但我想让它更容易阅读,如果我有数据像1000000000
我希望它显示1,000,000,000
所以我在mysql中使用这个查询;
format(col_name,0)
我尝试在c#中使用gridview排序函数来排序,我使用这个来排序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 |
+================+=================+
我该如何修复它?
第一解决方案:
在c#代码中进行格式化。
int num = 11111111;
string s = num.ToString("N0");
第二方案:
在sql查询中包括原始的int列以及格式化的值&对原始的int列&
一个解决方案可能是在您的mySQL查询中,您将同时拥有Data1
和Data2
的格式化版本和未格式化版本,然后根据未格式化版本对其进行排序,因此您的查询看起来像这样:
SELECT Data1 as A, format(Data1,0) as 'Data1',
Data2 as B, format(Data2,0) as 'Data2'
FROM `tabletest`
然后,如果用户单击使用Data1
排序,那么您将在'A'
(以上数据a1的别名)中排序,而不是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' (Data2
),如:
String newSort = tempExp == 'Data1' > 'A' : 'B'; // Use the Alias to sort
sortedView.Sort = newSort + " Desc";
然后从用户的眼睛隐藏排序列,但一定要把它放在它被分配给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